python - Python一一转换目录中的文件
问题描述
如何使用下面的代码一一转换目录中的所有文件?
此代码获取文件夹中的所有文件并将它们一起转换,但占用了太多内存。我需要分别在每个文件的循环中执行此操作。
即查找文件。兑换。移动。重复。
import os
import shutil
import glob
command = ('convert -compress LZW -alpha off -density 320 -depth 4 -
contrast-stretch 700x0 -gamma .45455 *.pdf -set filename:base "%
[basename]" +adjoin "%[filename:base].tiff"')
newpath = r'...'
new_dir = 'tiff'
if not os.path.exists(newpath):
try:
os.mkdir(new_dir)
os.system(command)
except:
print "The folder is already exist"
for file in glob.glob("*.tiff"):
try:
print('"' + file + '"' + ' has just moved to ' + '"' + new_dir + '"' + ' folder')
shutil.move(file, new_dir);
except:
print "Error"
解决方案
使用重命名?
import os
os.mkdir("new_folder")
for file in ['file1.txt', 'file2.txt']:
os.rename(file,f'new_folder/{file}')
推荐阅读
- php - 如何将 PHP-FPM 与别名目录一起使用
- python - Python过滤字典列表
- reactjs - React js按钮刷新并加载新数据集
- kendo-ui - 剑道网格中的动态加载器?
- javascript - 将结果映射到反转 ReactJS
- lambda - 如何将 lambda 表达式传递给使用反射调用的方法
- c# - 如何使用 NPOI Excel .NET 获取依赖单元格
- git - 如何在经历了多次提交的文件中查看开始和结束之间的更改?
- android - FileOutputStream 上的文件不是文件异常 - 从移动应用程序下载文件 - android
- r - 如何为R中的变量分配颜色?