我有一个脚本,可以在上传文件到FTP之前重命名文件。首先它搜索模式“_768x432_1700_m30_768x432”,如果找到,则模式将替换为“new”—然后将目录中的所有“.mp4”文件上载到FTP服务器。但是由于某种原因,我似乎不能删除上传后的文件?还有没有更好的方法来完成这个脚本?(我对python相当陌生)
#!/usr/bin/python
import os
import glob
import fnmatch
import sys
import ftplib
import shutil
import re
from ftplib import FTP
Host='xxxxxx.xxxxx.xxxx.com'
User='xxxxxxx'
Passwd='xxxxxxx'
ftp = ftplib.FTP(Host,User,Passwd) # Connect
dest_dir = '/8619/_!/xxxx/xx/xxxxx/xxxxxx/xxxx/'
Origin_dir = '/8619/_!/xxxx/xx/xxxxx/xxxxxx/xxxx/'
pattern = '*.mp4'
file_list = os.listdir(Origin_dir)
for filename in glob.glob(os.path.join(Origin_dir, "*_768x432_1700_m30_*")):
os.rename(filename, filename.replace('_768x432_1700_m30_','_new_' ))
video_list = fnmatch.filter(filename, pattern)
print(video_list)
print "Checking %s for files" % Origin_dir
for files in file_list:
if fnmatch.fnmatch(files, pattern):
print(files)
print "logging into %s FTP" % Host
ftp = FTP(Host)
ftp.login(User, Passwd)
ftp.cwd(dest_dir)
print "uploading files to %s" % Host
ftp.storbinary('STOR ' + dest_dir+files, open(Origin_dir+files, "rb"), 1024)
ftp.close
print 'FTP connection has been closed'
在下一行
ftp.storbinary('STOR ' + dest_dir+files, open(Origin_dir+files, "rb"), 1024)
打开一个文件,但不保留对它的引用并关闭它。在Windows上(我假设您是在Windows上运行的),当进程打开文件时,不能删除它。在请尝试以下方法:
区别在于:
open()
调用的结果分配给一个名称(f
)ftp.close()
中添加了缺少的括号,因此函数被调用。在相关问题 更多 >
编程相关推荐