使用 Python 脚本查找和重命名文件
Find and rename files using a Python script
我是 Python 编码的新手,所以这里有一个问题。我想找到名为 "untitled" 且具有任何扩展名的文件,例如jpg, 工业, psd.然后将它们重命名为当天的日期。
我试过以下方法:
import os
for file in os.listdir("/Users/shirin/Desktop/Artez"):
if file.endswith("untitled.*"):
print(file)
当我 运行 脚本时,没有任何反应。
您可能会发现 glob
函数在这种情况下更有用:
import glob
for file in glob.glob("/Users/shirin/Desktop/Artez/untitled.*"):
print(file)
您的函数不会打印任何内容,因为名称中可能没有以 .*
结尾的文件。 glob.glob()
函数将为您进行文件扩展
然后您可以使用它来重命名文件,如下所示:
import glob
import os
from datetime import datetime
current_day = datetime.now().strftime("%Y-%m-%d")
for source_name in glob.glob("/Users/shirin/Desktop/Artez/untitled.*"):
path, fullname = os.path.split(source_name)
basename, ext = os.path.splitext(fullname)
target_name = os.path.join(path, '{}{}'.format(current_day, ext))
os.rename(source_name, target_name)
一个Python datetime
对象可以用来给你一个合适的时间戳。
Python 字符串比较器不支持通配符。可以在文中任意位置搜索"untitled.":
import os
for file in os.listdir("/Users/shirin/Desktop/Artez"):
if "untitled." in file:
print(file)
请记住,这将包括在文件的任何位置具有 "untitled." 的任何文件。
试试这个方法
import os
directoryPath = '/Users/shirin/Desktop/Artez'
lstDir = os.walk(directoryPath)
for root, dirs, files in lstDir:
for fichero in files:
(filename, extension) = os.path.splitext(fichero)
if filename.find('untitle') != -1: # == 0 if starting with untitle
os.system('mv '+directoryPath+filename+extension+' '+directoryPath+'$(date +"%Y_%m_%d")'+filename+extension)
import os
for file in os.listdir("/Users/shirin/Desktop/Artez"):
if(file.startswith("untitled")):
os.rename(file, datetime.date.today().strftime("%B %d, %Y") + "." + file.split(".")[-1])
我是 Python 编码的新手,所以这里有一个问题。我想找到名为 "untitled" 且具有任何扩展名的文件,例如jpg, 工业, psd.然后将它们重命名为当天的日期。
我试过以下方法:
import os
for file in os.listdir("/Users/shirin/Desktop/Artez"):
if file.endswith("untitled.*"):
print(file)
当我 运行 脚本时,没有任何反应。
您可能会发现 glob
函数在这种情况下更有用:
import glob
for file in glob.glob("/Users/shirin/Desktop/Artez/untitled.*"):
print(file)
您的函数不会打印任何内容,因为名称中可能没有以 .*
结尾的文件。 glob.glob()
函数将为您进行文件扩展
然后您可以使用它来重命名文件,如下所示:
import glob
import os
from datetime import datetime
current_day = datetime.now().strftime("%Y-%m-%d")
for source_name in glob.glob("/Users/shirin/Desktop/Artez/untitled.*"):
path, fullname = os.path.split(source_name)
basename, ext = os.path.splitext(fullname)
target_name = os.path.join(path, '{}{}'.format(current_day, ext))
os.rename(source_name, target_name)
一个Python datetime
对象可以用来给你一个合适的时间戳。
Python 字符串比较器不支持通配符。可以在文中任意位置搜索"untitled.":
import os
for file in os.listdir("/Users/shirin/Desktop/Artez"):
if "untitled." in file:
print(file)
请记住,这将包括在文件的任何位置具有 "untitled." 的任何文件。
试试这个方法
import os
directoryPath = '/Users/shirin/Desktop/Artez'
lstDir = os.walk(directoryPath)
for root, dirs, files in lstDir:
for fichero in files:
(filename, extension) = os.path.splitext(fichero)
if filename.find('untitle') != -1: # == 0 if starting with untitle
os.system('mv '+directoryPath+filename+extension+' '+directoryPath+'$(date +"%Y_%m_%d")'+filename+extension)
import os
for file in os.listdir("/Users/shirin/Desktop/Artez"):
if(file.startswith("untitled")):
os.rename(file, datetime.date.today().strftime("%B %d, %Y") + "." + file.split(".")[-1])