Python's os module:
Import# operating system type
If it is POSIX , it indicates that the system is Linux
, Unix
or Mac OS X
, if it is NT , is the Windows
system.
# Operating System Details
The uname () function is not available on Windows, which means that some functions of the OS module are operating system-related.
Os.environ # Environment variables defined in the operating system
Os.environ.get ('path') # Gets an environment variable defined in the operating system, where ' path '
os
It is important to note that part of the function for manipulating files and directories is placed in the module and part of the os.path
module.
# to view the absolute path of the current directory:os.path.abspath ('. ' ) '/users/yhjin '
# To create a new directory under a directory, first represent the full path to the new directory:new_file_path = Os.path.join ('/users/' ) TestDir'
'/users/yhjin/testdir '
# then create a directory:
Os.mkdir (New_file_path
# Remove a directory:
Os.rmdir (New_file_path)
* Note: When combining two paths, do not spell the string directly, but pass the os.path.join () function so that the path delimiter for the different operating systems can be handled correctly. Under Linux/unix/mac, os.path.join () returns such a string:
Part-1/part-2
and Windows will return such a string:
Part-1\part-2
The os.path.split () function, splits a path into two parts, and the latter part is always the last level of the directory or file name:
Os.path.split ('/users/yhjin/test') ('/users/yhjin') 'test')
The os.path.splitext () function, splits a path into two parts, and the latter part is the file extension (note that the band '. ') 's):
Os.path.splitext ('/path/to/file.txt')
('/path/to/file ', '. txt ')
* Note: These combined, split-path functions do not require directories and files to be real, they operate only on strings.
# to rename a file: os.rename ('/users/yhjin/testdir/test.log ','/users/yhjin/testdir/test.txt') # Erase File:os.remove ('/users/yhjin/testdir/test.txt ')
os 模块无法复制文件,需要复制文件可以使用 shutil
The copyfile () function of the module.
Some other common methods:
# lists all file names and folder names under the specified path os.listdir ('. ')
# determines whether the specified path is a folder Os.path.isdir ('/users/yhjin/testdir') True
# determines whether the specified path is a file os.path.isfile ('/users/yhjin/testdir')
Some chestnuts:
# get all folder names under the current path for in Os.listdir ('. ' if os.path.isdir (x)]
# gets all the. py files under the current path for in Os.listdir ('. ' if os.path.isfile (x) and Os.path.splitext (x) [1]=='. py ']
Mixed chestnuts:
Finds the file name containing the specified string in the current directory and all subdirectories of the current directory, and prints a relative path.
For example, the above file structure, find all the file name with ' test ' in the test directory, and print out the relative path.
ImportOspathout= Os.path.abspath ('.')defSearch (path, search_text): Files= [x forXinchOs.listdir (PATH)ifos.path.isfile (Os.path.join (path, x))] forIinchFiles:ifSearch_textinchI:Print(Os.path.join (path, i)) dirs= [x forXinchOs.listdir (PATH)ifOs.path.isdir (Os.path.join (path, x))] forIinchDirs:search (os.path.join (path, i), search_text) search (Pathout,'Test')#It is important to note that isfile and Isdir must be the complete path + file, so you need to use the Os.path.join method instead of just passing in the X
The output is as follows:
python-Manipulating files and directories