Python deletes a file instance of a specified type (or not specified) in detail

Source: Internet
Author: User
This example analyzes Python's deletion of a specified type (or unspecified) file usage. Share to everyone for your reference. Specific as follows:
Below, delete the non-source files in the directory

Import OS import string Def del_files (dir,topdown=true):   for Root, dirs, files in Os.walk (dir, topdown): for     name In files:       pathname = Os.path.splitext (Os.path.join (root, name))       if (pathname[1]! = ". cpp" and pathname[1]! = ". HP P "and pathname[1]! =". H "):         Os.remove (Os.path.join (root, name))         

The above function, traverse the folder can also use function Os.listdir (dirname). It is only slightly different from the way Os.walk (dir, Topdown).
Listdir is named according to the rules, folders and files, unified use of depth-first search method, enumerated
The standard routine of Os.walk is to traverse the file first and then traverse the folder.

Learning Essentials:

Functions of the OS class:

The Os.getenv () and os.putenv () functions are used to read and set environment variables, respectively.
The Os.system () function is used to run the shell command.
The OS.LINESEP string gives the line terminator used by the current platform. For example, Windows uses ' \ r \ n ', Linux uses ' \ n ' and Mac uses ' \ R '.

path-related OS functions

Os.listdir (dirname): List directories and files under DirName
OS.GETCWD (): Gets the current working directory, which is the directory path of the current Python script work.
Os.curdir: Returns the current directory ('. ')
Os.chdir (dirname): Changing the working directory to DirName

Os.path.isdir (name): Determine if Name is a directory, name is not a directory and return false
Os.path.isfile (name): Determine if name is not a file, does not exist name also returns false
Os.path.exists (name): Determine if there is a file or directory name

Os.path.getsize (name): Gets the file size if name is directory returned 0
Os.path.abspath (name): Get absolute path
Os.path.normpath (PATH): Canonical path string form

Os.path.split (name): Split file name and directory (in fact, if you use the directory completely, it will also separate the last directory as the file name, and it will not determine whether the file or directory exists)
>>> os.path.split ('/home/swaroop/byte/code/poem.txt ')
('/home/swaroop/byte/code ', ' poem.txt ')
Os.path.splitext (): Detach file name and extension

Os.rename (name1, name2) renaming files
such as modifying the file type, Os.rename (Os.path.join (root, name), pathname[0]+ ". cpp") Pathname[0] is the file name, Pathname[1] is the extension

Os.path.join (path,name): Connection directory with file name or directory
Os.path.basename (PATH): Return file name
Os.path.dirname (PATH): Return file path

Os.walk return ternary form, equivalent to the ternary list, traverse path, return an object, each of his parts is a ternary group, (' Directory x ', [Directory x under Directories list], directory x below the file)

String type of data, you can use = =,! = and other operators

Multi-reference Python help documentation, very powerful.

Hopefully this article will help you with Python programming.

  • 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.