Python file path

Source: Internet
Author: User

The Python programming language can help us easily implement some special functional requirements. Here we will give you a detailed introduction to the related operations on the Python file path, so that we can get some help in actual development.

Python file path Operation Method 1: OS. listdir (path) // path is the Directory

This function is equivalent to executing the dir command in the path directory and returns the list type. Example:

 
 
  1. print os.listdir(’..’) 

Output:

 
 
  1. [a,b,c,d] 

Python file path Operation Method 2: OS. path. walk (path, visit, arg)

Path: the directory to be traversed

Visit: is a function pointer, and the circle of the function is:

 
 
  1. callback(arg,dir,fileList) 

Here, arg is the arg passed to the walk, dir is a directory under path, and fileList is a list composed of files and directories under dir.

Arg: used for transmission to visit. It has no effect on the walk.

Example:

 
 
  1. def callback(arg,directory, files):  
  2. print directory,  
  3. print files,  
  4. print arg  
  5. print ‘——————–’  
  6. os.path.walk(’.',callback, ‘123456′) 

Output:

 
 
  1. . ['path0704.py', 'temp', '\xc2\xb7\xbe\xb6\xcf\xe0\xb9\
    xd8\xd1\xa7\xcf\xb0.txt'] 123456  
  2. ——————–  
  3. .\temp ['temp.h', 'temp1'] 123456  
  4. ——————–  
  5. .\temp\temp1 ['abc.bmp'] 123456 

To find all the files in a directory, you only need to find the files in fileList in callback.

In addition, there is also a function that can be used, that is, OS. walk. See 10.

Python file path operation method 3: OS. path. split (path)

The path is a path and output. The path is divided into two parts. For details, see the example:

 
 
  1. print os.path.split(”abc/de.txt”)  
  2. (’abc’, ‘de.txt’)  
  3. os.path.split(”abc”)  
  4. (”, ‘abc’)  
  5. print os.path.split(”de/abc/de”)  
  6. (’de/abc’, ‘de’) 

Python file path operation method 4: OS. path. splitext (filename)

Divide the file name into file names and extensions

 
 
  1. os.path.splitext(abc/abcd.txt)  
  2. (’abc/abcd’, ‘.txt’) 

Python file path Operation Method 5: OS. path. dirname (path)

Put forward the directory name

 
 
  1. Print OS. path. dirname ("abc ")
  2. # The output is empty.
  3. Print OS. path. dirname ('abc \ def ')
  4. Abc

6: OS. path. basename (filename)

Get the main file name

 
 
  1. Print OS. path. basename ('abc ')
  2. Abc
  3. Print OS .path.basename('abc.txt ')
  4. Abc
  5. Print OS. path. basename ('bcd/abc ')
  6. Abc # This does not include the directory name.
  7. Print OS. path. basename ('.')

Python file path Operation Method 7: OS. mkdir (path, [mode])

  • Python inheritance embodies object-oriented features
  • Main steps for Python to call. net framework
  • Python coding experience in creating Silverlight controls
  • Python parsing XML correct application code example
  • Analysis of Python image Optimization Techniques

Path indicates the directory name. You can only create a level-1 directory. For example, if path is abc/def, abc must exist in the current directory; otherwise, it fails.

Python file path Operation Method 8: OS. makedirs (path [, mode])

You can create multi-level directories.

Python file path Operation Method 9: OS. remove (path)

Deleting an object must be an object.

 
 
  1. OS. removedirs (path) delete all objects in a directory
  2. OS. rmdir (path) to delete a directory, which must be empty; otherwise, OS. errer

Python file path operation method 10: OS. walk (path)

Traverse the path and return an object. Each part of the object is a triple ('Directory x', [directory list under directory x], and files under directory x)

Example:

 
 
  1. a = os.walk(’.')  
  2. for i in a:  
  3. print i 

Output:

 
 
  1. (’.', ['abc', 'temp'], ['path0704.py', '\xc2\xb7\xbe\xb6\xcf\
    xe0\xb9\xd8\xd1\xa7\xcf\xb0.txt'])  
  2. (’.\\abc’, [], ['\xd0\xc2\xbd\xa8 BMP \xcd\xbc\xcf\xf1.bmp'])  
  3. (’.\\temp’, ['temp1'], ['temp.h'])  
  4. (’.\\temp\\temp1′, [], ['abc.bmp']) 

11: shutil. copy (src, dst)

Copy the src content of the file to the dst file ., The destination region must be writable. If dst exists, dst is overwritten.

The above Python file path function is basically enough

For other file movement operations, see: shutil module: High-level file operations.

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.