The Python programming language can help us easily implement some special functional requirements. Here we will give you a detailed introduction to the related operations on the Python file path, so that we can get some help in actual development.
Python file path Operation Method 1: OS. listdir (path) // path is the Directory
This function is equivalent to executing the dir command in the path directory and returns the list type. Example:
- print os.listdir(’..’)
Output:
- [a,b,c,d]
Python file path Operation Method 2: OS. path. walk (path, visit, arg)
Path: the directory to be traversed
Visit: is a function pointer, and the circle of the function is:
- callback(arg,dir,fileList)
Here, arg is the arg passed to the walk, dir is a directory under path, and fileList is a list composed of files and directories under dir.
Arg: used for transmission to visit. It has no effect on the walk.
Example:
- def callback(arg,directory, files):
- print directory,
- print files,
- print arg
- print ‘——————–’
- os.path.walk(’.',callback, ‘123456′)
Output:
- . ['path0704.py', 'temp', '\xc2\xb7\xbe\xb6\xcf\xe0\xb9\
xd8\xd1\xa7\xcf\xb0.txt'] 123456
- ——————–
- .\temp ['temp.h', 'temp1'] 123456
- ——————–
- .\temp\temp1 ['abc.bmp'] 123456
To find all the files in a directory, you only need to find the files in fileList in callback.
In addition, there is also a function that can be used, that is, OS. walk. See 10.
Python file path operation method 3: OS. path. split (path)
The path is a path and output. The path is divided into two parts. For details, see the example:
- print os.path.split(”abc/de.txt”)
- (’abc’, ‘de.txt’)
- os.path.split(”abc”)
- (”, ‘abc’)
- print os.path.split(”de/abc/de”)
- (’de/abc’, ‘de’)
Python file path operation method 4: OS. path. splitext (filename)
Divide the file name into file names and extensions
- os.path.splitext(abc/abcd.txt)
- (’abc/abcd’, ‘.txt’)
Python file path Operation Method 5: OS. path. dirname (path)
Put forward the directory name
- Print OS. path. dirname ("abc ")
- # The output is empty.
- Print OS. path. dirname ('abc \ def ')
- Abc
6: OS. path. basename (filename)
Get the main file name
- Print OS. path. basename ('abc ')
- Abc
- Print OS .path.basename('abc.txt ')
- Abc
- Print OS. path. basename ('bcd/abc ')
- Abc # This does not include the directory name.
- Print OS. path. basename ('.')
Python file path Operation Method 7: OS. mkdir (path, [mode])
- Python inheritance embodies object-oriented features
- Main steps for Python to call. net framework
- Python coding experience in creating Silverlight controls
- Python parsing XML correct application code example
- Analysis of Python image Optimization Techniques
Path indicates the directory name. You can only create a level-1 directory. For example, if path is abc/def, abc must exist in the current directory; otherwise, it fails.
Python file path Operation Method 8: OS. makedirs (path [, mode])
You can create multi-level directories.
Python file path Operation Method 9: OS. remove (path)
Deleting an object must be an object.
- OS. removedirs (path) delete all objects in a directory
- OS. rmdir (path) to delete a directory, which must be empty; otherwise, OS. errer
Python file path operation method 10: OS. walk (path)
Traverse the path and return an object. Each part of the object is a triple ('Directory x', [directory list under directory x], and files under directory x)
Example:
- a = os.walk(’.')
- for i in a:
- print i
Output:
- (’.', ['abc', 'temp'], ['path0704.py', '\xc2\xb7\xbe\xb6\xcf\
xe0\xb9\xd8\xd1\xa7\xcf\xb0.txt'])
- (’.\\abc’, [], ['\xd0\xc2\xbd\xa8 BMP \xcd\xbc\xcf\xf1.bmp'])
- (’.\\temp’, ['temp1'], ['temp.h'])
- (’.\\temp\\temp1′, [], ['abc.bmp'])
11: shutil. copy (src, dst)
Copy the src content of the file to the dst file ., The destination region must be writable. If dst exists, dst is overwritten.
The above Python file path function is basically enough
For other file movement operations, see: shutil module: High-level file operations.