# This is a learning note for the Liaoche teacher Python tutorial
1. Overview
The OS module can directly invoke the interface functions provided by the operating system. Help us manipulate directories and files in a Python program.
part of the function for manipulating files and directories is placed in the OS module, a section is placed in the Os.path in the module
However, the function of copying the file does not exist in the OS module because the copy file is not a system call provided by the operating system. However, the shutil module provides functions for CopyFile () , and you can find many useful functions in the shutil module, which can be seen as additions to the OS module.
1. Basic Functions
>>> Import OS
>>> Os.name # operating system type
' POSIX '
if it is POSIX, the system is Linux, UNIX, or Mac OS X, if NT, is the Windows system.
To get detailed system information, you can call the uname () function
2. Environment Variables
The environment variables defined in the operating system are all stored in the os.environ variable and can be viewed directly
>>> Os.environ
To get the value of an environment variable, you can call os.environ.get (' key ')
>>> os.environ.get (' PATH ')
'/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/x11/bin:/usr/local/mysql/bin '
>>> os.environ.get (' x ', ' Default ')
' Default '
3. Operation Catalogue
# View the absolute path of the current directory:
>>> os.path.abspath ('. ')
'/users/michael '
# Create a new directory under a directory and first represent the full path to the new directory:
# When synthesizing two paths, do not spell the string directly, but pass the Os.path.join () function
# This will correctly handle path separators for different operating systems.
>>> os.path.join ('/users/michael ', ' TestDir ')
'/users/michael/testdir '
# then create a directory:
>>> os.mkdir ('/users/michael/testdir ')
# Remove a directory:
>>> os.rmdir ('/users/michael/testdir ')
4, the operation of the path
# split Path,os.path.split () function
>>> os.path.split ('/users/michael/testdir/file.txt ')
('/users/michael/testdir ', ' file.txt ') # The latter part is always the last level of the directory or file name
# get file extension,Os.path.splitext ()
>>> os.path.splitext ('/path/to/file.txt ')
('/path/to/file ', '. txt ')
5. Operation Files
# Rename the file:
>>> os.rename (' test.txt ', ' test.py ')
# Delete Files:
>>> os.remove (' test.py ')
6. Filter files with Python features
# List all directories in the current directory
>>> [x for X in Os.listdir ('. ') if Os.path.isdir (x)] # Traverse all directories
['. Lein ', '. Local ', '. M2 ', '. npm ', '. ssh ', '. Trash ', '. Vim ', ' applications ', ' Desktop ', ...]
# list all the . PY file
>>> [x for X in Os.listdir ('. ') if Os.path.isfile (x) and Os.path.splitext (x) [1]== '. Py ']
[' apis.py ', ' config.py ', ' models.py ', ' pymonitor.py ', ' test_db.py ', ' urls.py ', ' wsgiapp.py ']
2. Example
1. Use OS module to write an implementation dir-l the output program.
Import Os,time
Currentdir = OS.GETCWD () #获取路径
FileNum, filesize, dirnum = 0, 0, 0 #初始计量
For name in Os.listdir (currentdir):
If Os.path.isfile (name): #判断文件是否存在
Print ('%s\t\t%d\t%s '% (time.strftime ('%y/%m/%d%h:%m ', Time.localtime (os.path.getmtime (name))), Os.path.getsize ( Name)) #打印文件的时间, size, name
FileNum + = 1 #文件数量 +1
FileSize + = os.path.getsize (name) # File size accumulation
Elif Os.path.isdir (name): #判断目录是否存在
Print ('%s\t<dir>\t\t%s '% (time.strftime ('%y/%m/%d%h:%m ', Time.localtime (name)), name)) # Time to print the catalog, name
Dirnum + = 1 #目录数量 +1
Print (' \t\t%d files \t\t%d bytes '% (FileNum, filesize))
Print (' \t\t%d directory '% of Dirnum)
2, write a program, in the current directory and all subdirectories of the current directory to find the file name contains the specified string of files, and print out the relative path.
Import OS
def search (dststr, path=['. '): #用相对路径
For n in Path:
Dirlist = [x for x in Os.listdir (n) if Os.path.isdir (Os.path.join (n, x))] #找出子目录名
filelist = [x for x in Os.listdir (n) if Os.path.isfile (Os.path.join (n, x)) and Dststr in Os.path.splitext (x ) [0]] #找出含关键字的文件名
If Len (filelist) > 0:
Print (n, ': ')
For M in FileList:
Print (m)
Print (' \ n ')
Path = [Os.path.join (n, x) for x in Dirlist]
Search (dststr, path)
Return ' Done '
Dststr = ' 2 '
Search (DSTSTR)
Python Learning notes __9.3 operations files and directories