Import OS
1. OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work
Print (OS.GETCWD ()) output:c:\users\jmaly\desktop\python\test
2, Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD
Print (OS.GETCWD ()) Os.chdir ("C:\\users\jmaly\desktop\python") print (OS.GETCWD ()) output:c:\users\jmaly\desktop\ Python\testc:\users\jmaly\desktop\python
3, Os.curdir return the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Print (os.curdir) print (os.pardir) output: ...
4, Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directory
Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on
Os.makedirs ("c:\\users\\jmaly\\desktop\\python\\test\\123\\456") os.removedirs ("c:\\users\\jmaly\\desktop\\ python\\test\\123\\456 ")
5, Os.mkdir (' dirname ') generate a single-level directory, equivalent to the shell mkdir dirname
Os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.mkdir ("c:\\users\\jmaly\\desktop\\python\\test\\123") Os.rmdir ("c:\\users\\jmaly\\desktop\\python\\test\\123 ")
6, Os.remove () delete a file
Os.remove ("testting.py")
7. The Os.name output string indicates the current use of the platform. Win-> ' NT '; Linux-> ' POSIX '
Print (Os.name) output:nt
8, Os.rename ("Oldname", "newname") Rename File/directory
Os.renames ("testting.py", "test.py")
9, Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Print (Os.listdir ("./")) output:[' Data.json ', ' decrator.py ', ' decrator2.py ', ' func.py ', ' json-module.py ', ' random-module.py ', ' time-module.py ', ' Verification code.py ', ' __init__.py ', ' __pycache__ ']
9, Os.stat (' path/filename ') get File/directory information
Print (Os.stat ("./test.py")) Output:os.stat_result (st_mode=33206, st_ino=3377699720559409, st_dev=385350305, St_ Nlink=1, St_uid=0, St_gid=0, st_size=0, st_atime=1507605962, st_mtime=1507605962, st_ctime=1507605962)
10, OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/"
OS.LINESEP output The line terminator used by the current platform, win under "\t\n", Linux "\ n"
OS.PATHSEP output string for splitting the file path
11, Os.system ("Bash command") runs the shell command, directly displays
12. Os.environ Get System environment variable
Print (Os.environ) Output:environ ({' AllUsersProfile ': ' C:\\programdata ', ' APPDATA ': ' c:\\users\\jmaly\\appdata\\ Roaming ', ' commonprogramfiles ': ' C:\\Program Files (x86) \\Common files ', ' CommonProgramFiles (X86) ': ' C:\\Program Files (x86) \\Common files ', ' COMMONPROGRAMW6432 ': ' C:\\Program Files\\Common Files ', ' COMPUTERNAME ': ' MARTIN ', ' COMSPEC ': ' C : \\WINDOWS\\system32\\cmd.exe ', ' fps_browser_app_profile_string ': ' Internet Explorer ', ' Fps_browser_user_profile_ STRING ': ' Default ', ' homedrive ': ' C: ', ' HomePath ': ' \\Users\\JMALY ', ' localappdata ': ' c:\\users\\jmaly\\appdata\\ Local ', ' logonserver ': ' \\\\martin ', ' moz_plugin_path ': ' C:\\Program Files (x86) \\Foxit Software\\foxit reader\\ Plugins\\ ', ' number_of_processors ': ' 4 ', ' ONEDRIVE ': ' c:\\users\\jmaly\\onedrive ', ' OS ': ' Windows_NT ', ' PATH ': ' c:\\ Windows\\System32; C:\\Windows; C:\\windows\\system32\\wbem; c:\\windows\\system32\\windowspowershell\\v1.0\\; C:\\Program Files (x86) \\NVIDIA Corporation\\physx\\common; C:\\Windows\\System32; C:\\Windows; C:\\windows\\system32\\wbem; c:\\windows\\system32\\windowspowershell\\v1.0\\; C:\\Program Files\\git\\cmd; C:\\users\\jmaly\\appdata\\local\\microsoft\\windowsapps; ', ' pathext ': '. COM;. EXE;. BAT;. CMD;. VBS;. VBE;. JS;. JSE;. WSF;. WSH;. MSC ', ' processor_architecture ': ' x86 ', ' processor_architew6432 ': ' AMD64 ', ' processor_identifier ': ' Intel64 Family 6 Model 158 Stepping 9, Genuineintel ', ' processor_level ': ' 6 ', ' processor_revision ': ' 9e09 ', ' PROGRAMDATA ': ' c:\\ ProgramData ', ' ProgramFiles ': ' C:\\Program Files (x86) ', ' ProgramFiles (X86) ': ' C:\\Program Files (x86) ', ' PROGRAMW6432 ' : ' C:\\Program Files ', ' psmodulepath ': ' C:\\Program Files\\windowspowershell\\modules; C:\\windows\\system32\\windowspowershell\\v1.0\\modules ', ' Public ': ' C:\\users\\public ', ' pycharm_hosted ': ' 1 ', ' Pythonioencoding ': ' UTF-8 ', ' PYTHONPATH ': ' C:\\users\\jmaly\\desktop\\python ', ' pythonunbuffered ': ' 1 ', ' sessionname ': ' Console ', ' systemdrive ': ' C: ', ' SYSTEMROOT ': ' C:\\Windows ', ' TEMP ': ' C:\\users\\jmaly\\appdata\\local\\temp', ' TMP ': ' c:\\users\\jmaly\\appdata\\local\\temp ', ' userdomain ': ' Martin ', ' Userdomain_roamingprofile ': ' Martin ', ' USERNAME ': ' jmaly ', ' userprofile ': ' C:\\users\\jmaly ', ' windir ': ' C:\\Windows '}
13, Os.path.abspath (path) returns the absolute path normalized by path
os.path.split (path) splits path into directory and file name two tuples returned
os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)
os.path.basename (path) returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path)
os.path.exists (path) returns true if path exists, false if path does not exist
os.path.isabs (path) returns True if path is an absolute path
os.path.isfile (path) returns True if path is a file that exists. Otherwise returns false
os.path.isdir (path) returns True if path is a directory that exists. Otherwise returns false
Os.path.join (path1[, path2[, ...]) returns a combination of multiple paths, and the parameters before the first absolute path are ignored
os.path.getatime (path) returns the last access time of the file or directory to which path is pointing
os.path.getmtime (path) returns the last modified time of the file or directory to which path is pointing
Python OS module