我有一个脚本,在将文件上传到FTP之前对其进行重命名。首先搜索模式“_768x432_1700_m30_”,如果发现它被替换为“新” - 那么它会将目录中的所有“.mp4”文件上传到FTP服务器。但由于某种原因,我似乎无法删除文件上传后的文件?还有更好的方法来做这个脚本? (我是相当新的Python)Python重命名ftp上传文件删除
#!/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.close()'而不是'ftp.close'吗? – mgilson 2012-07-30 15:12:57
嗨 - 当我尝试删除“文件”收集目录中的所有* .mp4文件并上载它们的变量时 - 我似乎无法弄清楚如何在上传后删除文件。所以,如果我尝试os.remove(文件)脚本刚刚关闭 – JRM 2012-07-30 16:16:13
因此,我希望脚本删除文件上传后。对不起,我没有更清楚 – JRM 2012-07-30 16:17:38