python 文件操作基础-1

  • python os 基础操作文件命令相关
import os

# 增加文件夹
# r代表路径中不存在转义字符
# os.mkdir(r'/Users/jiahongming/Documents/Testwork/PythonStudy')#创建单个文件夹
# os.mkdir('/Users/jiahongming/Documents/Testwork/PythonStudy02')#创建单个文件夹

# 相对路径,相对于当前py文件,.代表当前文件夹,..代表上一级文件夹
# os.mkdir("./jhy")
# os.mkdir("../jhy")
# os.mkdir("../../jhy02")
# os.makedirs("path")
# os.makedirs("./jhy/jhy01/python")
# # 删除文件或者文件夹操作
# os.remove("path")
# os.remove("./jhy/jhy01/python/1.txt")
# os.rmdir("path")
# os.rmdir("./jhy/jhy01/python")
# os.removedirs("path")
# os.removedirs("./jhy/jhy01/python")
# 判断是否是一个文件
print(os.path.isfile("../jhy"))
print(os.path.isfile("../jhy/testfile.py"))
# 判断是否是文件夹
print(os.path.isdir("../jhy"))
print(os.path.isdir("../jhy/testfile.py"))
# 判断是否存在
print(os.path.exists("../jhy/testfile.py"))
print(os.path.exists("../jhy/testfile01.py"))

猜你喜欢

转载自blog.csdn.net/u013279518/article/details/89088971