Python3 learning the Nineth bullet: module Learning II File Management module

Source: Internet
Author: User

OS Module
Provides access to the operating system interface 1> name obtains the current operating system where ' NT ' is a dictionary of Windows ' POSIX ' is linux2> environ obtains the current system's environment variables (variable name: variable value) 3> GETCWD () Get current working directory 4> Getgid () returns the real group of the current process id5> GetLogin () returns the currently logged in User name 6> Getpid () returns the current process's pid7> system (command) Running the shell command under Linux is running the terminal, under Windows is running CMD, so the command is different because it can run the shell command, it can be used to open software such as "c:\\" there is a browser >>> os.system (R ' C : \firefox.exe ') by so you can open the Firefox browser return value is only 0 (success),1,28> popen (command) is almost the same as the system function, except that Popen can return the value obtained by the command, often used for ' piping ' 9 > startfile (PATH) runs directly under the path path software # The following commands are almost identical to the file management commands under Linux under Terminal Control 10> mkdir (path) creates a new directory, equivalent to the Linux $mkdir command 11> RmDir (path) Delete empty directory, equivalent to Linux under the $rmdir command 12> list (path) to return all files and folders in the path directory, the equivalent of Linux under the $ls command 13> remove (path) Delete files, Equivalent to Linux under the $RM command 14> rename (src, DST) rename files, src and DST are named before and after the file path, that is, not only can rename the file, but also can change the path of the file, equivalent to cut 15> chmod ( path, mode) changes the permissions of the file, equivalent to Linux under the $chmod command 16> chown (path, UID, GID) to change the file owner and the owning group, equivalent to the Linux under the $chown command 17> stat (PATH) View the properties of the file, return the value of St_atime (Access time), St_mtime (modified time), St_ctime (creation time), st_size (file Size/byte) equivalent to Linux under $ls-l command 18> Symlink (SRC, DST) for paths to src files on path DST create soft links, equivalent to Linux under $LN-S command add:17> Walk (path, Topdown = True, onerror = None) recursively traverse path directory a) every time Calendar to a directory, return triples (file directory absolute path, current directory folder, current directory file) b) Topdown is true, indicates that the current directory is traversed before the file, and then traverse the current directory subdirectory. Conversely, when Topdown is false, it iterates through the subdirectories of the current directory, returns the files under the subdirectory, and finally returns the file C in the current directory, onerror to none, indicating that errors generated when the file traversal is ignored, or you can customize the Add function to operate after the error message is prompted > >> Path = R ' D:\\test ' >>> for root, dirs, files in Os.walk (path) print (root, dirs, files)
Os.path Module
There are many functions that deal with long file names (pathname) (or you do not need to handle the front and back slashes) 1> basename (path) removes the directory path, returns the file name 2> dirname (path), removes the file name, and returns the directory path 3> split (path Returns the tuple 4> splitext (path, filename) of a tuple that returns (file name, extension) 5> join (path, *path) gives path to all filename generation path >>> os.path.join (' ... ') , ' A ', ' B ', ' new.txt ') '. \\a\\b\\new.txt ' # Check file name (path) characteristics 6> exists (paths) determine if a file or directory exists 7> isdir (path) to determine whether the file is 8> for the folder Isfile 9> (path) Islink (path) determines whether a link, if the system does not support the link form, returns false10> Isabs (path) to determine whether the absolute path 11> abspath (path) returns the absolute path of the file # file information 12> GetSize (path) return file size 13> getatime (path) return file last accessed time 14> getctime (path) return file creation time 15> getmtime (path) Return file last modified time return time can be used to get standard output time in Gmtime 16> call some path identifiers (mostly Linux standard)    altsep = '/'    CurDir = '. '    Defpath = R '.; C:\bin '    devnull = ' nul '    extsep = '. '    Pardir = ' ... '    Pathsep = '; '    Sep = R ' \ '    supports_unicode_filenames = True
Shutil Module
Provides a high level of file operation interface 1> copy (SRC, DST) copy files, from SRC to DST, equivalent to Linux under $CP command 2> move (SRC, DSC) move files, from Src to DST, equivalent to Linux under $MV command

  

Python3 learning the Nineth bullet: module Learning II File Management module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.