"""对某一路径下的某种类型文件进行批量重命名"""
import os
#文件路径,注意用/或\,不能用
path=‘D:\CrawledData’
#返回指定的文件夹包含的文件或文件夹的名字的列表
file_list=os.listdir(path)
n=0
for file_obj in file_list:
#针对某一种文件,比如.jpg文件
if file_obj.endswith(’.jpg’):
#之前的文件名
src=os.path.join(path,file_obj)
#根据自己的需要设置新文件名。format中{:0>5d}的含义:数字补零 (填充左边, 宽度为5)
newname = ‘{:0>5d}.jpg’.format(n)
dst = os.path.join(path, newname)
#用os模块中的rename方法对文件改名
os.rename(src,dst)
print(src,’======>’,dst)
n+=1
python对某一路径下的某种类型文件进行批量重命名 - stt12345678的博客 - CSDN博客
https://blog.csdn.net/stt12345678/article/details/80492490
图片批量重命名.py-CSDN下载
https://download.csdn.net/download/qq_40584593/11176439
python 删除文件、目录 - MuWinter的博客 - CSDN博客
https://blog.csdn.net/muwinter/article/details/77196261
#使用os.unlink()和os.remove()来删除文件
#!/user/local/bin/python2.7
# -*- coding:utf-8 -*-
import os
my_file = 'D:/text.txt'
if os.path.exists(my_file):
#删除文件,可使用以下两种方法。
os.remove(my_file)
#os.unlink(my_file)
else:
print 'no such file:%s'%my_file
python怎么把字符串转换成数字
s1 = '123'
s1
'123'
i1 = int(s1)
i1
123
s2 = '123.4'
f2 = float(s2)
f2
123.4
创建文件与目录
使用os库函数实现。
import os
save_dir = "save_path/"
if os.path.exists(save_dir) is False:
os.makedirs(save_dir)
检测是否有这个文件目录,不存在的话,会自动创建。
自动创建文件(包含没有文件父目录,自动创建)
public void test1() throws IOException {
File file = new File(“E:\app\1111\12312\1212132312.html”);
// 如果文件夹不存在则创建
if (!file.getParentFile().exists()&&!file.isDirectory()){
file.getParentFile().mkdirs();
file.createNewFile();
} else {
file.createNewFile();
}
}