Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the file directory of operation and maintenance process in Python

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "Python how to compile the operation and maintenance process file directory". In the daily operation, I believe that many people have doubts about how to write the operation and maintenance process file directory in Python. The editor consulted all kinds of materials and sorted out a simple and easy-to-use method of operation. I hope it will be helpful to answer the doubt of "how to compile the operation and maintenance process file directory by Python". Next, please follow the editor to study!

1. Execute an external program or command

We have the following C language program cal.c (compiled into a .out file), which is responsible for entering two command line arguments and printing their sum. The program needs to use Python to call the C language program and check whether the program returns normally (normal return will return 0).

# include#includeint main (int argc, char* argv []) {int a = atoi (argv [1]); int b = atoi (argv [2]); int c = a + b; printf ("% d +% d =% d\ n", a, b, c); return 0;}

Then we can use the run function of the subprocess module to spawn a child process:

Res = subprocess.run (["Python-Lang/cal.out", "1", "2"]) print (res.returncode)

You can see that the console prints out the return value of the process 0:

1 + 2 = 3

0

Of course, if the program is killed halfway. If we write the following while.c program as the following endless loop (compiled into a .out file):

# include#includeint main (int argc, char* argv []) {while (1); return 0;}

We also use the run function to receive the return value:

Res = subprocess.run ("Python-Lang/while.out") print (res.returncode)

However, we terminate it with the shell command while the program is running:

(base) orion-orion@MacBook-Pro Python-Lang% ps-a | grep while 11829 ttys001 0ps 17.49 Python-Lang/while.out11891 ttys005 00.00 grep while (base) orion-orion@MacBook-Pro Python-Lang% kill 11829

You can see that the process return value of the console printout is-15 (because a negative value of-N indicates that the child process is terminated by signal N, while the default signal of the kill command is 15, which terminates the process):

-15

If the program is stuck in a dead loop and can't be terminated properly, we can't wait forever, can we? At this point, we can set the timeout mechanism and catch exceptions:

Try: res = subprocess.run (["Python-Lang/while.out"], capture_output=True, timeout=5) except subprocess.TimeoutExpired as e: print (e)

The exception result is printed:

Command'['Python-Lang/while.out']' timed out after 5 seconds

Sometimes you need to get the output of the program. You can add the capture_output parameter at this time, and then access the stdout property of the returned object:

Res = subprocess.run (["netstat", "- a"], capture_output=True) out_bytes = res.stdout

The output is returned as a byte string. If you want to interpret it as text, you can add another decoding step:

Out_text = out_bytes.decode ("utf-8") print (out_text)

You can see that the output in text form has been obtained normally:

Kctl 0 0 33 6 com.apple.netsrckctl 0 0 34 6 com.apple.netsrckctl 0 0 1 7 com.apple.network.statisticskctl 0 0 2 7 com.apple.network.statisticskctl 0 0 37 com.apple.network.statistics (base) orion-orion@MacBook-Pro Learn-Python%

Generally speaking, the execution of the command does not depend on the support of the underlying shell (such as sh,bash, etc.), and the string list we provide is passed directly to the underlying system call, such as os.execve (). If you want the command to be executed through shell, all you need to do is give the parameter shell=True and provide the command as a simple string. For example, when we want Python to execute a Shell command that involves piping, Imax O redirection, or other complexity, we can write:

Out_bytes = subprocess.run ("ps-a | wc-l > out", shell=True) 2. File and directory operations (naming, deleting, copying, moving, etc.)

When we want to deal with file names and paths, to ensure optimal portability (especially if we need to run with both Unix and Windows), it's best to use functions in os.path. For example:

Import osfile_name = "/ Users/orion-orion/Documents/LocalCode/Learn-Python/Python-Lang/test.txt" print (os.path.basename (file_name)) # test.txtprint (os.path.dirname (file_name)) # / Users/orion-orion/Documents/LocalCode/Learn-Python/Python-Langprint (os.path.split (file_name)) # ('/ Users/orion-orion/Documents/LocalCode/Learn-Python/Python-Lang') 'test.txt') print (os.path.join ("/ new/dir", os.path.basename (file_name) # / new/dir/test.txtprint (os.path.expanduser ("~ / Documents")) # / Users/orion-orion/Documents

Where os.path.expanduser does nothing when the user or $HOME is unknown. For example, our $HOME here is / Users/orion-orion:

(base) orion-orion@MacBook-Pro ~% echo $HOME/Users/orion-orion

If you want to delete a file, use os.remove (be careful to determine whether the file exists before deleting):

File_name = "Python-Lang/test.txt" if os.path.exists (file_name): os.remove (file_name)

Next, let's look at how to copy files. Of course, the most direct way is to call the Shell command:

Os.system ("cp Python-Lang/test.txt Python-Lang/test2.txt")

Of course it's not elegant enough. If you don't do this by invoking the shell command, you can use the shutil module, which provides a series of high-level operations on files and file collections, including file copying and moving / renaming. The arguments to these functions are strings that provide the name of the file or directory. The following is an example:

Src = "Python-Lang/test.txt" dst = "Python-Lang/test2.txt" # corresponding to cp src dst (copy file Overwrite if it exists) shutil.copy (src, dst) src = "Python-Lang/sub_dir" dst = "Python-Lang/sub_dir2" # corresponding to cp-R src dst (copy the entire directory tree) shutil.copytree (src, dst) src = "Python-Lang/test.txt" dst = "Python-Lang/sub_dir/test2.txt" # corresponding to mv src dst (mobile files, you can choose whether to rename) shutil.move (src, dst)

As you can see, as the comments say, the semantics of these functions are similar to the Unix command.

By default, if the source file is a symbolic link, the target file will be a copy of the file that the link points to. If you only want to copy the symbolic link itself, you can provide the keyword parameter follow_symlinks:

Shutil.copy (src, dst, follow_symlinks=True)

If you want to keep symbolic links in the copied directory, you can do this:

Shutil.copytree (src, dst, symlinks=True)

Sometimes you need to ignore specific files and directories when copying an entire directory, such as intermediate process bytecode such as .pyc. We can provide copytree with an ignore function that takes the directory name and file name as input parameters and returns a list of names to ignore as a result (here we use the .endswith method of the string object, which is used to get the file type):

Def ignore_pyc_files (dirname, filenames): return [name for name in filenames if name.endswith ('pyc')] shutil.copytree (src, dst, ignore=ignore_pyc_files)

However, because the pattern of ignoring filenames is very common, a utility function ignore_patterns () has been provided for us to use (the related pattern usage is similar to .gitignore):

Shutil.copytree (src, dst, ignore=shutil.ignore_patterns ("* ~", "* .pyc"))

Note: the "* ~" pattern matching here is an intermediate file that ends with "~" produced by a text editor such as Vi.

Ignoring file names is also often used in os.listdir (). For example, in data-intensive (such as machine learning) applications, we need to traverse all dataset files in the data directory and load them, but we need to exclude them. A hidden file at the beginning, such as .git, otherwise it will make an error, you can write it in the following ways:

Import osimport osfilenames = [filename for filename in os.listdir ("Python-Lang/data") if not filename.startswith (".")] # Note that os.listdir returns a file name without a path

Let's go back to copytree (). One of the thornier problems when copying directories with copytree () is error handling. For example, some files can not be accessed due to permission problems, such as encountered damaged symbolic links in the process of copying. In this case, all encountered exceptions are collected into a list and grouped into a separate exception, which is thrown at the end of the operation. Examples are as follows:

Import shutilsrc = "Python-Lang/sub_dir" dst = "Python-Lang/sub_dir2" try: shutil.copytree (src, dst) except shutil.Error as e: for src, dst, msg in e.args [0]: print (src, dst, msg)

If ignore_dangling_symlinks=True is provided, copytree will ignore overhanging symbolic links.

More information about the use of shutil (such as logging, file permissions, etc.) can be found in the shutil documentation [4].

Next let's see how to use the os.walk () function to traverse the hierarchical directory to search for files. You just need to provide it with the top-level directory. For example, the following function is used to find a specific file name and print out the absolute paths of all matching results:

Import osdef findfile (start, name): for relpath, dirs, files in os.walk (start): if name in files: # print (relpath) full_path = os.path.abspath (os.path.join (relpath, name)) print (full_path) start = "." name = "test.txt" findfile (start, name)

As you can see, os.walk can traverse the directory level for us, and it returns a triple for each directory level it enters, including: the relative path of the directory being viewed (relative to the script execution path), the list of all directory names contained in the directory being viewed, and the list of all file names contained in the directory being viewed. The os.path.abspath here accepts a possible relative path and forms it into an absolute path.

We can also make the script perform more complex functions in addition, such as the following function to print out all the files that have been recently modified:

Import osimport timedef modified_within (start, seconds): now = time.time () for relpath, dirs, files in os.walk (start): for name in files: full_path = os.path.join (relpath, name) mtime = os.path.getmtime (full_path) if mtime > (now-seconds): print (full_path) start = "." seconds = 60modified_within (start, 60) 3 Create and unpack an archive file

If you just want to create or unpack the archive, you can directly use the high-level functions in the shutil module:

Import shutilshutil.make_archive (base_name= "data", format= "zip", root_dir= "Python-Lang/data") shutil.unpack_archive ("data.zip")

The second parameter, format, is the format of the desired output. To get a list of supported archive formats, you can use the get_archive_formats () function:

Print (shutil.get_archive_formats ()) # [('bztar', "bzip2'ed tar-file"), (' gztar', "gzip'ed tar-file"), ('tar',' uncompressed tar file'), ('xztar', "xz'ed tar-file"), (' zip', 'ZIP file')]

Python also provides modules such as tarfile, zipfile, gzip, and so on, to handle the underlying details of the archive format. For example, if we want to create and unpack a .zip archive file, we can write:

Import zipfilewith zipfile.ZipFile ('Python-Lang/data.zip',') as zout: zout.write (filename='Python-Lang/data/test1.txt', arcname= "test1.txt") zout.write (filename='Python-Lang/data/test2.txt', arcname= "test2.txt") with zipfile.ZipFile ('Python-Lang/data.zip', 'r') as zin: zin.extractall (' Python-Lang/data2') # if not, the data2 directory is automatically created here On the "Python how to compile the operation and maintenance process file directory" study is over, I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report