Python directory file

Source: Internet
Author: User

Write a little every day, one day I can become more salty salted

Python in the operation of the file and directory is basically dependent on the Os,shutil module, which takes the main OS module, the most important examples of several methods are as follows:

1. Determine if the file/directory exists (os.path.exists (filename)), with the following example:

  

Returns true if the file exists, false if not present

2. Get the current file path (OS.GETCWD ()) with the following example:

3. Delete the file (Os.remove ()) with the following example:

Delete a file to ensure that the file does exist

4. Modify the File/directory name (Os.rename ()) with the following example:

Modify file name needs to determine the existence of files

5. Traverse all the files in the directory (Os.walk), as shown in the following example:

1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportOS4 5 6  forDirs,paths,namesinchOs.walk (OS.GETCWD ()):7      forPathinchpaths:8         PrintPath9 Ten      forNameinchnames: One         PrintOs.path.join (Dirs,path,name)

The output is as follows:

1 . idea2 D:\test_his\.idea\a.txt3 D:\test_his\.idea\b.txt4 D:\test_his\.idea\main.py5 D:\test_his\.idea\scrpy.py6 D:\test_his\.idea\test.py7 D:\test_his\.idea\test1.py8 Inspectionprofiles9 D:\test_his\.idea\inspectionProfiles\encodings.xmlTen D:\test_his\.idea\inspectionProfiles\misc.xml One D:\test_his\.idea\inspectionProfiles\modules.xml A D:\TEST_HIS\.IDEA\INSPECTIONPROFILES\TEST_HIS.IML - D:\test_his\.idea\inspectionProfiles\workspace.xml -D:\test_his\.idea\inspectionProfiles\inspectionProfiles\profiles_settings.xml

The rest of the methods and functions are described below:

Name Role Note
Os.listdir (Filedir) Returns all file names and directory names under the specified directory Directory exists
Os.removedirs (R ' Filedir ') Delete multiple directories Directory exists
Os.path.getsize (filename) Get File size
Os.path.splitext (filename) Detach suffix Name Detach the last. The front and back contents of the symbol
Os.path.isfile () Determine if the file is
Os.path.isdir () Determine if the directory
Os.path.split () Separating file directories and filenames
Os.path.dirname () Get path name
Os.path.islink () Whether there is a link
Os.mkdir () Create a Directory
Os.makedirs () Create multiple Catalogs
Os.chmod () Modify Permissions
Os.stat Get file properties
Shutil.copyfile () Copy files
Shutil.copy (File,path) Copy files to Directory
Shutil.copytree (Path,newpath) Copy Entire Directory
Shutil.move () Move files or directories
Shutil.rmtree (dir) Delete Directory

Python directory file

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.