Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD
Os.curdir returns the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.makedirs (' dirname1/dirname2 ') can generate multi-level recursive directories
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.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell
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.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.remove () Delete a file
Os.rename ("Oldname", "newname") renaming files/directories
Os.stat (' path/filename ') get File/directory information
Os.symlink (' Path/filename ', ' ln_filename ') create symbolic links, source required absolute path
Os.utime () Modify Time Properties >>> Import os>>> stinfo = Os.stat (' c.py ') >>> print "Access duration of c.py:%s \ n Modified time of c.py:%s "% (stinfo.st_atime,stinfo.st_mtime) access time of c.py:1375448908.0modified time of c.py:1369 735909.0>>> os.utime (' c.py ', (1375448978,1369735977)) >>> print "Access time of c.py:%s \nmodified Time of c.py:%s "% (stinfo.st_atime,stinfo.st_mtime) access time of c.py:1375448908.0modified time of c.py:1369735909.0 fallback Out of Python Interactive mode, enter >>> import again os>>> stinfo = Os.stat (' c.py ') >>> print "Access time of C . PY:%s \nmodified time of c.py:%s "% (stinfo.st_atime,stinfo.st_mtime) access time of c.py:1375448978.0modified time of c.py:1369735977.0
Os.walk () generates all filenames under a directory tree
Os.walk (top[, topdown=true[, onerror=none[, Followlinks=false]])
- Top indicates the path of the directory tree that needs to be traversed
- The default value of Topdown is "True", which means that the files under the directory tree are returned first, and then the subdirectories of the directory tree are traversed. When the value of Topdown is "False", it means traversing a subdirectory of the directory tree, returning the file under the subdirectory, and finally returning the file under the root directory
- The default value for OnError is "None", which means that errors resulting from file traversal are ignored. If not NULL, provide a custom function tip error message after you continue traversing or throwing an exception to abort the traversal
The function returns a tuple that has 3 elements, each of which represents the path name of each traversal, the directory list, and the file list.
Os.walk () Example:
>>> import os>>> for root, dirs, files in Os.walk ("Wd/chat", Topdown=false): ... for name in FILES:...
print (Os.path.join (root, name)) #打印文件绝对路径 ... for name in dirs: ... Print (Os.path.join (root, name)) #打印目录绝对路径 ...
Os.tmpfile () Create and open ' w+b ' a new temporary file
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
The Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Bash command") runs a shell command that directly displays
Os.popen ("Bash command") runs shell commands, generates objects, assigns variables, and then reads >>> imports os>>> os.system (' ls twisted ') Chat _client_twisted.py chat_server_twisted.py0 >>> LS = Os.popen (' LS twisted ') >>> ls.readlines () [' Chat_ Client_twisted.py\n ', ' chat_server_twisted.py\n ']
Os.environ Getting system environment variables
Os.access (' Pathfile ', OS. W_OK) Verify file permission mode, Output True,false
Os.chmod (' Pathfile ', OS. W_OK) Changing the file permission mode
# echo ' Test ' > Test.sh>>> os.access (' test.sh ', OS. W_OK) true>>> os.access (' test.sh ', OS. X_OK) false>>> os.chmod (' test.sh ', OS. X_OK) >>> os.access (' test.sh ', OS. X_OK) true# ls-l test.sh---------x 1 root root Oct 23:03 test.sh
Os.path Common Modules Detailed
Os.path.abspath (path) returns the path normalized absolute path >>> import os.path>>> os.path.abspath (' c.py ') '/root/py/c.py ' >>> Os.path.abspath ('.. /py/c.py ') '/root/py/c.py '
Os.path.split (path) divides path into directory and file name two tuples return >>> os.path.split ('/root/py/c.py ') ('/root/py ', ' c.py ') >> > Os.path.split ('/root/py/') ('/root/py ', ')
Os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path) >>> os.path.dirname ('/root/py/c.py ') '/root/py ' >>> os.path.dirname (' c.py ') '
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.basename ('/root/py/c.py ') ' c.py ' >>> os.path.basename ('/ Root/py ') ' py '
Os.path.commonprefix (list) returns the longest path common to all paths in the list, from left to right, same character
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.normcase (Path) under Linux, this function returns path as-is, all characters in the path are converted to lowercase on the Windows platform, and all slashes are converted to backslashes >>> Os.path.normcase ( ' c:/windows\\system32\\ ') ' c:\\windows\\system32\\ '
Os.path.normpath (path) canonical path >>> os.path.normpath (' c://windows\\system32\\. /temp/') ' C:\\Windows\\Temp '
Os.path.splitdrive (path) splits the drive name and path, primarily to win, to the Linux tuple first always empty >>> os.path.splitdrive (' C:\\Windows ') (' C: ', ' \ \ \ Windows ')
Os.path.splitext (path) separates the file name from the extension; the default return (fname,fextension) tuple, which can do the Shard operation with "." For delimiters >>> os.path.splitext ('/root/py/c.py ') ('/root/py/c ', '. Py ')
Os.path.getsize (path) returns the size of path (bytes)
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
Os.path.walk (Top,func,arg)
- Top indicates the path of the directory tree that needs to be traversed
- Func represents a callback function that processes a traversal path. The so-called callback function is used as a parameter of a function, and when a certain time is triggered, the program invokes a defined callback function to process a task. The callback function must provide 3 parameters: the 1th parameter is walk (), The 2nd parameter represents a list of directories, and the 3rd parameter represents a list of files
- Arg is a tuple that is passed to the callback parameter func. One parameter of the callback function must be ARG, which provides the processing parameters for the callback function. parameter arg can be null
>>> Import os>>> def visitdir (arg,dirname,names): ... for filespath in names: ... Print Os.path.join (dirname,filespath) ...>>> path= '/root/py/wd/chat ' >>> os.path.walk (path, Visitdir, ())/root/py/wd/chat/chat_server.py/root/py/wd/chat/chat_client.py/root/py/wd/chat/test/root/py/wd/ chat/test/linuxeye/root/py/wd/chat/test/test2/root/py/wd/chat/test/test3/root/py/wd/chat/test/test2/asdf/root/ Py/wd/chat/test/test3/sdfaxx
The list of file names produced by Os.path.walk () and Os.walk () is not the same. Os.path.walk () produces directory paths and file paths under the directory tree, while Os.walk () only produces file paths