Home  >  Article  >  Backend Development  >  python file path operation

python file path operation

巴扎黑
巴扎黑Original
2016-11-26 10:25:491479browse

import os.path

path = '/home/vamei/doc/file.txt'


print(os.path.basename(path)) #Query the file name contained in the path

print(os .path.dirname(path)) # Query the directory contained in the path


info = os.path.split(path) # Split the path into two parts: file name and directory, put them in a table and return

print(info)

path2 = os.path.join('/', 'home', 'vamei', 'doc', 'file1.txt') # Use the directory name and file name to form a path string, string Splicing output:/homevameidocfile1.txt


p_list = [path, path2]

print(os.path.commonprefix(p_list)) #Query the common parts of multiple paths


os.path.normpath (path) # Remove redundancy in the path. For example, '/home/vamei/../.' is converted to '/home'



print(os.path.exists(path)) # Check whether the file exists


print( os.path.getsize(path)) #Query the file size

print(os.path.getatime(path)) #Query the time when the file was last read

print(os.path.getmtime(path)) #Query The time when the file was last modified


print(os.path.isfile(path)) # Whether the path points to a regular file

print(os.path.isdir(path)) # Whether the path points to a directory file


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:python file managementNext article:python file management