[英]Python convert files in directory one by one
How do I convert all files in directory one by one with the code below?如何使用以下代码将目录中的所有文件一一转换?
This code takes all the files in a folder and converts them together, but uses up too much memory.这段代码将文件夹中的所有文件都转换在一起,但占用了太多内存。 I need to do it in the loop for each file separately.
我需要分别在每个文件的循环中执行此操作。
ie Find file.即查找文件。 Convert.
转变。 Move.
移动。 Repeat.
重复。
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"
using rename?使用重命名?
import os
os.mkdir("new_folder")
for file in ['file1.txt', 'file2.txt']:
os.rename(file,f'new_folder/{file}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.