Python System Study Notes (12) --- OS. path OS. walk

Source: Internet
Author: User

Obtain the current working directory, that is, the directory path of the current Python script: OS. getcwd () returns all files and directory names in the specified directory: OS. the listdir () function is used to delete a file: OS. remove () delete multiple directories: OS. removedirs (r "c: \ python") checks whether the given path is a file: OS. path. isfile () checks whether the given path is a directory: OS. path. isdir () determines whether it is an absolute path: OS. path. isabs () checks whether the given path is actually stored: OS. path. exists () returns the directory name and file name of a path: OS. path. split () eg OS. path. split ('/home/swaroop/byte/code/poem.txt') Result: ('/home/swaroop/byte/Code', 'poem.txt') OS. path. join (path, name): Connection directory and file name or object Directory separation Extension: OS. path. splitext () gets the path: OS. path. get the file name: OS. path. basename () Run shell command: OS. system () reads and sets the environment variable: OS. getenv () and OS. putenv () indicates the row Terminator used by the current platform: OS. linesep Windows uses '\ r \ n', Linux uses' \ n', and Mac uses '\ R' to indicate the platform you are using: OS. name is 'nt 'for Windows, while for Linux/Unix users, it is 'posix' renamed: OS. rename (old, new) creates a multilevel Directory: OS. makedirs (r "c: \ python \ test") to create a single directory: OS. mkdir ("test") gets the file attribute: OS. stat (file): OS. chmod (file) terminates the current process: OS. exit () to get the file size Small: OS. path. getsize (filename) OS module File/directory access function File Processing mkfifo ()/mknod () create named pipeline/Create File System node remove ()/unlink () delete file OS. the remove () function is used to delete an object. Rename ()/renames () rename file * stat () returns file information (including stat (), lstat (), xstat () symlink () Create Symbolic Link utime () update the timestamp tmpfile () to create and open ('W + B ') a new temporary file walk () to generate all the file name directories under the directory/folder chdir () /fchdir () change the current working directory/change the current working directory chroot () through a file descriptor change the root directory of the current process listdir () list the objects in the specified directory and return all objects and directory names in the specified directory. OS. listdir (dirname): list the directories under dirname and the getcwd ()/getcwdu () file. The current working directory/function is the same, but a Unicode object OS is returned. the getcwd () function obtains the current working directory, that is, the directory path of the current Python script. Mkdir ()/makedirs () create directory/create multi-level directory rmdir ()/removedirs () delete directory/delete multi-level Directory access/permission access () Check permission mode chmod () change permission mode chown ()/lchown () change owner and group ID/function is the same, but does not track link umask () Set default permission mode file descriptor operation open () underlying operating system open (for files, use the standard built-in open () function) read ()/write () read/write data dup ()/dup2 () according to the file descriptor () copy file description symbols/functions are the same, but are copied to another file descriptor device number makedev () create a original device number major ()/minor () from the major and minor device numbers () obtain the major/minor device number OS from the original device number. in the path module, the pathname access function separates the basename () Remove directory path, return file name dirname () Remove file name, return directory path join () combine separated parts into a path name split () Return (dirname (), basename () tuples splitdrive () returns (drivename, pathname) tuples splitext () returns (filename, extension) tuples information getatime () returns the most recent access time getctime () returns the file creation time. getmtime () returns the latest file modification time. getsize () returns the file size (in bytes). queries whether the specified path (file or directory) exists in isabs () of exists () specify whether the path is absolute path isdir () specify whether the path exists and whether the path exists for an isfile () directory and is an islink () File () whether the specified path exists and is a symbolic link ismount (). Whether the specified path exists and is a link Whether the path names of the samefile () are directed to the same file. Using the walk method provided by the traversal OS module, the path can traverse all directories and files under a given directory. Method: OS. walk (path), traverse the path, and return an object. Each part of it is a triple ('Directory x', [directory list under directory x], file under directory x) [python] import OS def export _dir (dir, topdown = True): for root, dirs, files in OS. walk (dir, topdown): for name in files: if (name. find ('graph ')! =-1): print name # print (OS. path. join (name) for name in dirs: # pass print (OS. path. join (name) # dir = raw_input ('Please input the path: ') dir = r "C: \ Users \ Administrator \ Desktop \ python_test \ AMD" 1__dir (dir)

Related Article

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.