Python Forum
'' FTP '' File upload with a specified string and rename
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'' FTP '' File upload with a specified string and rename
#1
Transfer a file in Local folder to a folder in FTP server and rename

I need to upload a file start with a specified string (0001) to an FTP server

My code it's working Only if give it the full filename = 0001_2655545XXXX

I need to upload a file if it is start with a string (0001) Without specifying the full name

import os, sys, stat
import ftplib
from ftplib import FTP

def ftpPush(src, filename , dst, new_name):
    ftp = FTP('xxxxxx', user='xxx', passwd='xxxx')
    ftp.cwd(dst)
    ftp.storlines("STOR "+filename, open(src+filename, 'rb'))
    ftp.rename(filename, new_name)
    ftp.quit()

src = '/' #path local
dst = '//'  #path server FTP

new_name = "Test1"
filename = []

for fileName in os.listdir(src):
    if filename.startswith('0001'):
       ftpPush(src, filename, dst, new_name)

print("File uploaded and renamed successfully!")
Reply
#2
Line 17 creates a list called filename
Line 19 assigns the current filename in the listdir to fileName
Line 20 tries to evaluate filename.startswith(), but there is no such method for a list. So this should exist with an exception.

I don't see how this would work if you gave the full filename at all. Is this the exact code that you ran?


Also line 8 uses src+filename, But that's liable to create a string that has no path separator between the directory and the filename. Preferable to use Path syntax here. I'd expect the open() to fail in that case.

Do you get any errors?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I upload a .gz file to the Swift Object Storage using Python swift client? Hanginium65 0 736 Jul-24-2024, 03:24 PM
Last Post: Hanginium65
  rename same file names in different directories elnk 5 2,398 Jul-12-2024, 01:43 PM
Last Post: snippsat
  Extract and rename a file from an Archive tester_V 4 3,630 Jul-08-2024, 07:54 AM
Last Post: tester_V
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 1,261 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  Rename first row in a CSV file James_S 3 1,578 Dec-17-2023, 05:20 AM
Last Post: James_S
  Need to replace a string with a file (HTML file) tester_V 1 1,897 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  rename file RolanRoll 0 1,073 May-18-2023, 02:17 PM
Last Post: RolanRoll
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,947 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk
  Unable to upload file using FTP korenron 2 3,579 Dec-23-2021, 12:44 PM
Last Post: korenron
  Rename Files based on XML file klturi421 3 3,438 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020