Get the current directory
import os print(os.getcwd())
#输出
C:\Users\admin\PycharmProjects\module
Change directory
import os os.chdir(r'C:\Users\admin\PycharmProjects') print(os.getcwd())
#输出
C:\Users\admin\PycharmProjects
Returns the current directory
os Import Print (os.curdir) # output .
Parent directory
OS Import Print (os.pardir) # output ..
Recursive directory build
import os os.makedirs(r"C:\a\b\c")
Recursive directory delete
Need only delete directory directory is empty.
import os os.removedirs(r"C:\a\b\c")
Build directory
Is not recursive
import os os.mkdir(r"C:\test")
Delete empty directories
It is not recursive.
import os os.rmdir(r"C:\test")
File in the folder listed in the directory
OS Import Print (the os.listdir (os.curdir)) # output [ 'main.py', 'module_test.py' , 'package', '__pycache__']
Delete a file
import os os.remove(r"C:\test.exe")
Renaming a file
import os os.rename(oldname,newname)
Get the file directory information
import os print(os.stat(os.curdir)) #输出 os.stat_result(st_mode=16895, st_ino=68961369294131359, st_dev=138692802, st_nlink=1, st_uid=0, st_gid=0, st_size=4096, st_atime=1581686008, st_mtime=1581686008, st_ctime=1581659080)
Acquiring path separators operating system
Windows path separators: \
Linux path separator: /
os Import Print (os.sep) # output \
Acquisition of the operating system line terminator
Under Windows:
OS Import Print (the repr (os.linesep)) # output '\ r \ n'
Get String operating system environment variables divided route
Windows:
OS Import Print (os.pathsep) # output ;
Linux is a colon.
View the current system environment variables
the amount print (os.environ)
Output string representing the current operating system
Windows
OS Import Print (The os.name) # output nt
Excuting an order
There Echo
import os os.system("dir")
Gets the absolute path
import os print(os.path.abspath(__file__)) #输出 C:\Users\admin\PycharmProjects\module\main.py
Split path and file
import os print(os.path.split(r"C:\Users\admin\PycharmProjects\module\main.py")) #输出 ('C:\\Users\\admin\\PycharmProjects\\module', 'main.py')
Get path pathname
import os print(os.path.dirname(r"C:\Users\admin\PycharmProjects\module\main.py")) #输出 C:\Users\admin\PycharmProjects\module
Get file path name
import os print(os.path.basename(r"C:\Users\admin\PycharmProjects\module\main.py")) #输出 main.py
Determining whether there is a specified path
import os print(os.path.exists(r"C:\Users\admin\PycharmProjects\module\main.py")) #输出 True
Determine whether it is an absolute path
import os print(os.path.isabs(r"C:\Users\admin\PycharmProjects\module\main.py")) #输出 True
Determine whether a file is
import os print(os.path.isfile(r"C:\Users\admin\PycharmProjects\module\main.py")) #输出 True
To determine whether a directory
import os print(os.path.isdir(r"C:\Users\admin\PycharmProjects\module")) #输出 True
Gets the last access time of file
import os print(os.path.getatime(r"C:\Users\admin\PycharmProjects\module\main.py"))
#输出
1581687585.792178
Get the file creation time
import os print(os.path.getctime(r"C:\Users\admin\PycharmProjects\shodan\module\main.py")) #输出 1581659101.4161289
Gets the last modified time of the file
import os print(os.path.getmtime(r"C:\Users\admin\PycharmProjects\shodan\module\main.py")) #输出 1581687695.1447003