簡體   English   中英

使用Python從ftp服務器下載文件

[英]download file from ftp server using Python

我正在嘗試從ftp服務器下載csv文件,但收到這些錯誤。 我不知道我要去哪里錯了。

#!/usr/bin/python

import ftplib


ftp = ftplib.FTP('192.168.0.00', 'bingo', 'word')
files = ftp.dir('/')
ftp.cwd("/")
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os

for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir,os.path.basename(filename))
    with open(target_file_name,'wb') as fhandle:
            ftp.retrbinary('RETR %s' %filename, fhandle.write))

errors:


sudo ./ftp_ringo.py
: not found.py: 1: ./ftp_ringo.py:
: not found.py: 3: ./ftp_ringo.py:
./ftp_ringo.py: 4: ./ftp_ringo.py: import: not found
: not found.py: 5: ./ftp_ringo.py:
./ftp_ringo.py: 6: ./ftp_ringo.py: Syntax error: "(" unexpected

這不是代碼的問題,問題出在您試圖調用它的方式上。 嘗試sudo python code.py或執行which python並在腳本中使用結果字符串而不是/usr/bin/python

這是我的更新,可以在下載文件時正常工作:

#!/usr/bin/python

import ftplib

ftp = ftplib.FTP('192.168.0.00', 'bingo', 'word')
files = ftp.dir('/')
ftp.cwd("/")
filematch = '*.csv'
target_dir = '/home/toor/ringolist'
import os

for filename in ftp.nlst(filematch):
    target_file_name = os.path.join(target_dir,os.path.basename(filename))
    with open(target_file_name,'wb') as fhandle:
            ftp.retrbinary('RETR %s' %filename, fhandle.write)


        # deletes the files from the FTP after transfer
            ftp.delete(os.path.basename(filename))

暫無
暫無

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

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