python实战 文件处理

Last updated on November 22, 2024 pm

🧙 Questions

使用python处理文件

☄️ Ideas

遍历文件夹
import os

path = 'D://'
for file in os.listdir(path):
	print('==>' + file )
创建文件夹
import os

newFloderPath = 'D://test'
os.mkdir(newFloderPath)
移动文件
import shutil

filePath='C:\\Users\\ispon\\Desktop\\demo'
folderPath='C:\\Users\\ispon\\Desktop\\test'
shutil.move(filePath,folderPath)
获取文件后缀
import os

filePath = 'C:\\Users\\ispon\\Desktop\\demo.py'
fileSuffix=os.path.splitext(filePath)[1].replace('.','')
print(fileSuffix)
读取txt文件信息
file=open('C:\\Users\\ispon\\Desktop\\demo.py')
line = file.readline()

while line:
	print(line.replace('\n',''))
	line = file.readline()
file.close()
拷贝文件
shutil.copyfile(src_path + file, target_path + file + '.png')

python实战 文件处理
https://ispong.isxcode.com/pytorch/python/python实战 文件处理/
Author
ispong
Posted on
March 24, 2022
Licensed under