修改图片文件的名称:拍摄时间
import os
import exifread
import time
import os.path
path = "D:\\新建文件夹 (2)\\"
path2 = "D:\\异常\\"
file_list = os.listdir(path)
for index in range(0, len(file_list)):
old_filename = file_list[index]
old_fullname = path + old_filename
tags = ""
print(old_filename)
with open(old_fullname,"rb") as jpegFile:
tags = exifread.process_file(jpegFile)
if 'EXIF DateTimeOriginal' in tags:
str_time = str(tags['EXIF DateTimeOriginal'])
timeStruct = ''
#print(str_time)
try:
if '上午' in str_time:
timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S上午")
elif '下午' in str_time:
timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S下午")
else:
timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S")
str_time = time.strftime("%Y%m%d_%H%M%S", timeStruct)
except ValueError as e:
print('ValueError:',old_filename,e)
new_filename = str_time + os.path.splitext(old_fullname)[1]
new_fullname = path + new_filename
if old_filename != new_filename:
try:
os.rename(old_fullname, new_fullname)
except IOError as e:
print('IOError:',old_filename,e)
os.rename(old_fullname, path2+old_filename)
else:
str_time = str(time.strftime("%Y%m%d_%H%M%S", time.localtime(os.path.getmtime(old_fullname))))
new_filename = str_time + os.path.splitext(old_fullname)[1]
new_fullname = os.path.join(path, new_filename)
if old_filename != new_filename:
try:
os.rename(old_fullname, new_fullname)
except IOError as e:
print('IOError:',old_filename,e)
os.rename(old_fullname, path2+old_filename)
修改图片文件的名称:拍摄时间+原文件名
import os
import exifread
import time
import os.path
path = "D:\\新建文件夹 (2)\\"
path2 = "D:\\异常\\"
file_list = os.listdir(path)
for index in range(0, len(file_list)):
old_filename = file_list[index]
# 用于拍摄日期重名 比较特殊,最终文件名为:拍摄日期+创建时间
old_filename2 = "__" + os.path.splitext(old_filename)[0]
old_fullname = path + old_filename
tags = ""
print(old_filename)
with open(old_fullname,"rb") as jpegFile:
tags = exifread.process_file(jpegFile)
if 'EXIF DateTimeOriginal' in tags:
str_time = str(tags['EXIF DateTimeOriginal'])
timeStruct = ''
#print(str_time)
try:
if '上午' in str_time:
timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S上午")
elif '下午' in str_time:
timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S下午")
else:
timeStruct = time.strptime(str_time, "%Y:%m:%d %H:%M:%S")
str_time = time.strftime("%Y%m%d_%H%M%S", timeStruct)
except ValueError as e:
print('ValueError:',old_filename,e)
new_filename = str_time + old_filename2 + os.path.splitext(old_fullname)[1]
new_fullname = path + new_filename
if old_filename != new_filename:
try:
os.rename(old_fullname, new_fullname)
except IOError as e:
print('IOError:',old_filename,e)
os.rename(old_fullname, path2+old_filename)
else:
str_time = str(time.strftime("%Y%m%d_%H%M%S", time.localtime(os.path.getmtime(old_fullname))))
new_filename = str_time + old_filename2 + os.path.splitext(old_fullname)[1]
new_fullname = os.path.join(path, new_filename)
if old_filename != new_filename:
try:
os.rename(old_fullname, new_fullname)
except IOError as e:
print('IOError:',old_filename,e)
os.rename(old_fullname, path2+old_filename)
修改图片文件的名称:创建时间
using System;
using System.IO;
namespace ConsoleApp1
{
public class Class14
{
public void test1()
{
string exeName =AppDomain.CurrentDomain.FriendlyName;
string path = Directory.GetCurrentDirectory();
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] inf = dir.GetFiles();
foreach (FileInfo fileInfo in inf)
{
Console.WriteLine(fileInfo.FullName);
if (fileInfo.Name.Equals(exeName))
{
continue;
}
string newPath = "";
string ext = Path.GetExtension(fileInfo.FullName);
string newName = fileInfo.CreationTime.ToString("yyyyMMdd_HHmmss_ffffff");
newPath = $"{fileInfo.DirectoryName}\\{newName}{ext}";
fileInfo.MoveTo(newPath);
}
}
}
}