Describes various modules in Python

Source: Internet
Author: User
Tags glob

The most abundant and powerful class libraries in the scripting language in Python are enough to support the vast majority of daily applications. In actual program development, Python is often called the assembly language, the reason for this is that he can easily use various modules made in other languages.

This article will explain in detail how to use these functions. First, we will introduce the file listing function in Python, which is similar to the dir command in Windows. Then describes how to test whether a file name corresponds to a standard file, directory, or link.

And how to extract the file size and date. We will also introduce how to delete files and directories. How to copy and delete files, and how to break down a complete file path into a directory part and a file name part. Finally, we will explain how to create a directory and how to move the directory in the directory tree and process the file.

1. Display directory content
You can use the glob module to complete this task when you want to list all files with an extension name of .jpg or .gif in the current directory, as shown below:

 
 
  1. import glob  
  2. filelist = glob 

The code above uses the glob function. The parameter of this function is the file type to be displayed. Here, the file type is specified by some file names similar to the shell-style wildcard descriptions in the UNIX operating system. For more information about how to use these wildcards, see the document of the fnmatch module. To display all files in a directory, you can use the OS. listdir function as follows:

 
 
  1. Files=OS. Listdir (r 'C: \ hpl \ scripting \ src \ py \ intro') # Applicable to Windows
  2. Files=OS. Listdir ('/home/hpl/scripting/src/py/intro') # Applicable to Unix
  3. # Cross-platform version:
  4. Files=OS. Listdir (OS. path. join (OS. environ ['scripting'],
  5. 'Src', 'py', 'intro '))
  6. Files=OS. Listdir (OS. curdir) # All files in the current directory
  7. Files=Glob. Glob ('*') + glob.

Ii. Test file type
We know that the file name, directory name, and link name all use a string as its identifier, but give us an identifier, how can we determine whether it refers to a regular file name, directory name, or link name? In this case, we can use the isfile function, isdir function, and islink function provided by the OS. path module to achieve our goal, as shown below:

 
 
  1. Print myfile, 'is ',
  2. If OS. path. isfile (myfile ):
  3. Print 'plain file'
  4. If OS. path. isdir (myfile ):
  5. Print 'Directory'
  6. If OS. path. islink (myfile ):
  7. Print 'link'
  8. You can also find the date and size of the file:
  9. Time_of_last_access=OS. Path. getatime (myfile)
  10. Time_of_last_modification=OS. Path. getmtime (myfile)
  11. Size=OS. Path. getsize (myfile)

Iii. Deletion of files and directories
To delete a single file, you can use the OS. remove function, for example, OS. remove ('mydata. dat '). The alias for OS. remove IS OS. unlink. However, the latter name is the same as the traditional UNIX operating system and the function for clearing files in Perl. You can delete a group of files by using the following formula, such as all files with the extension of .jpg and *. gif:

 
 
  1. Import stat
  2. Myfile_stat=OS. Stat (myfile)
  3. Size=Myfile_stat[Stat. ST_SIZE]
  4. Mode=Myfile_stat[Stat. ST_MODE]
  5. If stat. S_ISREG (mode ):
  6. Print '% (myfile) is a regular file with a size of % (size) d byte' % \
  7. Vars ()

As you know, you can use the rmdir command to delete the Python directory only when the content in the Python directory has been cleared. However, we often want to delete a directory tree containing many files. In this case, we can use the rmtree function provided by the shutil module, as shown below:

 
 
  1. If isinstance (files, str): # Is files a string?
  2. Files= [Files] # convert files from string to list
  3. If not isinstance (files, list): # Isn't files a list?
  4. For file in files:
  5. If OS. path. isdir (file ):
  6. Shutil. rmtree (file)
  7. Elif OS. path. isfile (file ):
  8. OS. remove (file)

The third parameter of Copytree specifies the processing of symbolic links. True indicates that the symbolic link is retained, and False indicates that the physical copy of the file replaces the symbolic link. The Python language supports cross-platform components of path names.

OS. path. the correct delimiter (used in UNIX and Mac OS x operating systems, and in Windows) can be used to join directories and file names. The variable OS. curdir and OS. pardir indicates the current working directory and its parent directory respectively. Like the following UNIX operating system commands

  1. Introduction to Python system files
  2. How to correctly use Python Functions
  3. Detailed introduction and analysis of Python build tools
  4. Advantages of Python in PythonAndroid
  5. How to Use the Python module to parse the configuration file?

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.