我有一個腳本,在將文件上傳到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