簡體   English   中英

python中ftp的os.renames

[英]os.renames for ftp in python

我想使用python將大量文件從Windows系統移至Unix ftp服務器。 我有一個csv,其中包含當前的完整路徑和文件名,以及將其發送到的新的基本地址(請參閱此處以獲取示例數據集)。

我有一個使用os.renames的腳本在Windows中進行傳輸和目錄創建,但是可以找到一種通過ftp輕松完成此操作的方法。

import os, glob, arcpy, csv, sys, shutil, datetime
top=os.getcwd()
RootOutput = top
startpath=top
FileList = csv.reader(open('FileList.csv'))
filecount=0
successcount=0
errorcount=0

# Copy/Move to FTP when required
ftp = ftplib.FTP('xxxxxx')
ftp.login('xxxx', 'xxxx')
directory = '/TransferredData'
ftp.cwd(directory)

##f = open(RootOutput+'\\Success_LOG.txt', 'a')
##f.write("Log of files Succesfully processed. RESULT of process run @:"+str(datetime.datetime.now())+"\n")
##f.close()
##
for File in FileList:
    infile=File[0]
    # local network ver
    #outfile=RootOutput+File[4]
    #os.renames(infile, outfile)

    # ftp netowrk ver
#    outfile=RootOutput+File[4]
#    ftp.mkd(directory)

    print infile, outfile

我在http://forums.arcgis.com/threads/17047-Upload-file-to-FTP-using-Python-ftplib中嘗試了該過程,但這是用於移動目錄中的所有文件的,新舊版本文件名,只需要它來創建中間目錄。

謝謝,

以下可能會起作用(未試用):

def mkpath(ftp, path):
    path = path.rsplit('/', 1)[0] # parent directory
    if not path:
        return
    try:
        ftp.cwd(path)
    except ftplib.error_perm:
        mkpath(ftp, path)
        ftp.mkd(path)

ftp = FTP(...)
directory = '/TransferredData/'

for File in FileList:
    infile = File[0]
    outfile = File[4].split('\\') # need forward slashes in FTP
    outfile = directory + '/'.join(outfile)
    mkpath(ftp, outfile)
    ftp.storbinary('STOR '+outfile, open(infile, 'rb'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM