python Road - a common module os

os module, os is the operating system and interactive modules

 

os.getcwd (): Gets the current working directory that the current directory path python script to work, if it is command-line mode, also said the current directory

os.listdir (path): Lists all files and subdirectories in the specified directory, including hidden files, and print as a list

os.mkdir ( 'dir1'): generating a single level directory, corresponding to the shell mkdir dirname

os.makedirs ( 'dir1 / dir2'): generating a multilayer recursive directory

os.rmdir ( 'dir1'): delete empty directories single-stage, can only delete empty folders, if the directory is not empty can not be deleted, an error; the equivalent of a shell rmdir dirname

os.removedirs ( 'dirname1'): If the directory is empty, delete, and recursively to the parent directory, should also empty, delete, and so on

os.remove (): Delete a file

os.rename ( "oldname", "newname"): Rename the file / directory

os.system ( "bash command"): Run shell command directly displayed; execute an operating system command, no return value, the actual operation

os.popen ( "bash command) .read () run shell command to obtain the execution result; return value

Difference os.system / os.popen similar with exec / eval

os.stat ( 'path / filename') get the file / directory information

 stat field descriptions:

st_mode: inode protection mode
st_ino: inode node number.
st_dev: inode device resides.
st_nlink: inode number of links.
st_uid: User ID of the owner.
st_gid: group ID of the owner.
st_size: common file unit size in bytes; wait for data containing special file.
st_atime: Time last accessed.
st_mtime: last modification time.
st_ctime: report by the operating system's "ctime". On some systems (such as Unix) is the time of most recent metadata changes, on the other systems (such as Windows) is to create a time

 

os.path

os.path.abspath with (path) returns the absolute path normalized path, an absolute path relative to the path, there may be no path

The os.path.split (path) to the directory path and file name into tuple returned to a path divided into two sections, and the second is a file / folder 

os.path.dirname (path) Returns the directory path, it is os.path.split (path) of the first element
os.path.basename (path) Returns the last path of the file name. How path to / \ or end, it will return a null value. That second element os.path.split (path) of

 # If you need two values ​​os.path.split

# If you just a value os.path.dirname / os.path.basename

os.path.exists (path) determine the file / folder exists, if the path exists, returns True, otherwise return False
os.path.isabs (path) to determine whether the path is an absolute path, a return True, otherwise return False
os .path.isfile (path) path determines whether the file exists, and if so returns True, otherwise return False
os.path.isdir (path) to determine whether the directory path exists, and if so returns True, otherwise return False
os.path .join (path1 [, path2 [, ...]]) the combination of a plurality of return paths, before the first parameter is ignored absolute path
os.path.getatime (path) returns the file or directory pointed to by path the last access time
os.path.getmtime (path) returns the file or directory path points to the last modification time
os.path.getsize (path) returns the size of path

os.sep output operation system-specific path separator is "\\" the win, is the Linux "/"
os.linesep output current platform using line terminator, to win the "\ r \ n", the Linux as "\ n"
string os.pathsep for dividing the output file path, the next win is;, under Linux is:
the os.name output string indicating the current use internet, win -> 'nt'; Linux -> 'posix '

 

Guess you like

Origin www.cnblogs.com/tester-star/p/11502580.html