当前位置: 移动技术网 > IT编程>脚本编程>Python > Python 编程常用文件操作实例

Python 编程常用文件操作实例

2020年07月07日  | 移动技术网IT编程  | 我要评论
1、判断文件夹是否存在,不存在则创建:import osif not os.path.exists(path): os.mkdir(path)2、遍历文件夹中有特定格式的文件:import fnmatchimg_list = fnmatch.filter(os.listdir(path), '*.png') for img in img_list : ……3、重命名文件:import osos.rename(Old_Dir,New_Dir)4、路径

1、判断文件夹是否存在,不存在则创建:

import os
if not os.path.exists(path):
    os.mkdir(path)

2、遍历文件夹中有特定格式的文件:

import os
import fnmatch
img_list = fnmatch.filter(os.listdir(path), '*.png')
for img in img_list :
    ……

3、重命名文件:

import os
os.rename(Old_Dir,New_Dir)

4、路径连接:

import os
New_Dir=os.path.join(path,filestr,filetype)

5、复制文件:

import shutil
shutil.copy(Old_Dir, New_Dir)

6、替换后缀名:

jpg_img=files[:-4]+".jpg"

7、替换路径中字符:

new_files=files.replace('_','')

 

本文地址:https://blog.csdn.net/X_Cosmic/article/details/107135035

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网