Python批量重命名指定文件夹下文件的两种方法

#法一
import os
path = "C://Python34//"
for file in os.listdir(path):
    if os.path.isfile(os.path.join(path,file))==True:
        if file.find('.')<0:
            newname=file+'.jpg'
            os.rename(os.path.join(path,file),os.path.join(path,newname))

#法二
import os
import glob

path = "C://Python34//"
for infile in glob.glob( os.path.join(path, '*.gif') ):
	for i in range (1,21):
		index_symbol = "_" + str(i) + "_"
		if index_symbol in infile:
			newfile = infile
			newfile = newfile.replace(index_symbol, str(i));
			os.rename(infile, newfile)
			print (infile)

时间: 2024-07-31 14:31:11

Python批量重命名指定文件夹下文件的两种方法的相关文章

php文件夹下文件批量重命名

php文件夹下文件批量重命名 <?php header("Content-type:text/html;charset=utf-8"); $dir = __DIR__.'./color/'; $file_arr = scandir($dir); unset($file_arr[0]); unset($file_arr[1]); $file_arr = array_values($file_arr); $n = count($file_arr); for ($i = 0; $i &

iOS NSFileManeger 计算文件是否超时,和计算文件夹下文件的总大小

//获得指定文件距离上次修改时间是否达到了指定值(秒)timeout +(BOOL)isTimeout:(NSString *)path time:(NSTimeInterval)timeout { //获得当前时间 NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path

android开发步步为营之54:读取assets,raw文件夹下文件

一.读取assets文件下文件products.json public String readAssetFile(Context c, String file) { Elapsed profiler = new Elapsed(); BufferedReader bufReader = null; try { InputStreamReader inputReader = new InputStreamReader(c.getResources().getAssets().open(file))

Linux统计某文件夹下文件、文件夹的个数

统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 如统计/home/han目录(包含子目录)下的所有js文件则: ls -lR /home/han|grep js|wc -l 或 ls -l "/home/han"|grep "js&qu

计算文件夹下文件的总大小

-(float)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小 {          NSFileManager *fileManager = [[NSFileManager alloc] init];     float size =0;     NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];     for(int i = 0; i<[array cou

Linux统计某文件夹下文件的个数

ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 统计/imagedata/data/20161108目录(包含子目录)下的所有txt文件 ls -lR /imagedata/data/20161108|grep txt|wc -l   或者  ls -lR "/imagedata/

Linux随笔 - Linux统计某文件夹下文件、文件夹的个数

统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 统计/imagedata/data/20161108目录(包含子目录)下的所有txt文件 ls -lR /imagedata/data/20161108|grep txt|wc -l   或者  ls -lR &quo

统计某文件夹下文件的个数

统计某文件夹下文件的个数ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的ls -lR|grep "^-"|wc -l 如统计/home/han目录(包含子目录)下的所有js文件则:ls -lR /home/han|grep js|wc -l 或 ls -l "/home/han"|grep "js"|

linux 统计文件夹下文件,文件夹,所有个数

统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 原文地址:https://www.cnblogs.com/dhName/p/11018555.html