How to copy and delete files in the Python language, and how to break down a complete file path into a directory part and a file name part. Finally, we will explain how to create a directory, and how to move directories and process files in the directory tree.
Although file and directory processing can be completed by operating system commands, the Python language allows developers to process related work in a programming manner, provides many built-in functions for processing files and directories. Importantly, these functions are used in the same way on Unix, Windows, and Macintosh platforms. This article will explain in detail how to use these functions.
First, we will introduce the file listing function in Python, which is similar to the dir command in Windows, and then describe how to test whether a file name corresponds to a standard file, directory, or link, and how to extract the file size and date.
After that, we will also introduce how to delete files and directories, copy and delete files, and how to break a complete file path into a directory part and a file name part. Finally, we will explain how to create directories and how to move directories and process files in the directory tree.
1. display the directory content when we want to list the contents in the current directory:
- Origdir=OS. Getcwd () # Write down the current location
- Newdir=OS. Path. join (OS. pardir, 'mynewdir ')
- If not OS. path. isdir (newdir ):
- OS. mkdir (newdir) # Or OS. mkdir (newdir, '123 ')
- OS. chdir (newdir)
- ...
- OS. chdir (origdir) # return to the original directory
- OS. chdir (OS. environ ['home']) # Move to the main directory
The code above uses the glob function. The parameter of this function is the file type to be displayed. Here, the file type is specified by some file names similar to the shell-style wildcard descriptions in the UNIX operating system. For more information about how to use these wildcards, see the document of the fnmatch module. To display all files in a directory, you can use the OS. listdir function as follows:
- Def checksize1 (arg, dirname, files ):
-
- For file in files:
-
- Filepath=OS. Path. join (dirname, file)
-
- If OS. path. isfile (filepath ):
-
- Size=OS. Path. getsize (filepath)
-
- If size>1000000:
-
- SizeSize_in_Mb= Size/1000000.0
-
- Arg. append (size_in_Mb, filename ))
-
- Bigfiles= []
-
- Root=OS. Environ ['home']
-
- OS. path. walk (root, checksize1, bigfiles)
-
- For size, name in bigfiles:
-
- Print name, 'size is ', size, 'mb'
We know that the file name, directory name, and link name all use a string as its identifier, but give us an identifier, how can we determine whether it refers to a regular file name, directory name, or link name? In this case, we can use the isfile function, isdir function, and islink function provided by the OS. path module to achieve our goal.
To delete a single file, you can use the OS. remove function, for example, OS. remove ('mydata. dat '). The alias for OS. remove IS OS. unlink. However, the latter name is the same as the traditional UNIX operating system and the function for clearing files in Perl. You can delete a group of files by using the following formula, such as all files with the extension of .jpg and *. gif:
- def somefunc(arg, dirname, files):
- origdir = os.getcwd(); os.chdir(dirname)
- <do tasks>
- os.chdir(origdir)
- os.path.walk(root, somefunc, arg)
As you know, you can use the rmdir command to delete a directory only when its content has been cleared. However, we often want to delete a directory tree containing many files. In this case, we can use the rmtree function provided by the shutil module, as shown below:
- def somefunc(arg, dirname, files):
- origdir = os.getcwd(); os.chdir(dirname)
- <do tasks>
- os.chdir(origdir)
- os.path.walk(root, somefunc, arg)
It is actually too strict. All we need is a file/directory name sequence that is traversed. In fact, we do not care whether the name is stored in a list, tuples, or numeric array. Therefore, a better test should be like the following:
Suppose we want to create a new directory py/src/test1 under our home directory, but neither py, src nor test1 currently exists. If you use the mkdir command to create an image. This nested directory can be created three times, but you do not need to worry about using the OS. makedirs Command provided by Python. This command can be used to create the entire directory at one time.
- Introduction to Python system files
- How to correctly use Python Functions
- Detailed introduction and analysis of Python build tools
- Advantages of Python in PythonAndroid
- How to Use the Python module to parse the configuration file?