Examples of file name and file path operations in python

黄舟
Release: 2017-10-03 05:41:39
Original
1661 people have browsed it

The following editor will bring you an example of python file name and file path operation. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

Readme:

In daily work, we often involve operations related to file names and file paths. In python, os The standard module provides us with various functions for file operations. This article will introduce respectively "get the current path", "get all files and folders under the current path", "delete files", "delete directories/multiple directories", "check File/File Path", "Check if file path exists", "Separate file path and file name", "Separate file extension", "Get file name" and Get "file path".


import os
'''获得当前路径
'''
cwd=os.getcwd()
print(cwd)
Copy after login


'''
得到当前文件夹下的所有文件和文件夹
'''
print(os.listdir())
Copy after login


'''
delete file
'''
os.remove('sw724.vaps')
print(os.listdir())
Copy after login


'''
删除单个目录和多个目录
'''
os.removedir()
os.removedir()
Copy after login


'''
检查是否是文件/文件夹
'''
print(os.path.isfile('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
print(os.path.isdir('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
Copy after login


'''
检查文件路径是否存在
'''

print(os.path.exists('/Users/liuxiaolong/PycharmProjects/untitled/iiii'))
Copy after login


'''
分离文件名
分离扩展名

'''
[dirname,filename]=os.path.split('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps')
print(dirname,"\n",filename)

[fname,fename]=os.path.splitext('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps')
print(fname,"\n",fename)
Copy after login


'''
获得文件路径
获得文件名
获得当前环境
'''
print("get pathname:",os.path.dirname('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
print("get filename:",os.path.basename('/Users/liuxiaolong/PycharmProjects/untitled/sw724.vaps'))
print(os.getenv)
Copy after login

The above is the detailed content of Examples of file name and file path operations in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!