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
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 284 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  Rename first row in a CSV file James_S 3 591 Dec-17-2023, 05:20 AM
Last Post: James_S
  Need to replace a string with a file (HTML file) tester_V 1 778 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  rename file RolanRoll 0 541 May-18-2023, 02:17 PM
Last Post: RolanRoll
  rename same file names in different directories elnk 0 719 Nov-04-2022, 05:23 PM
Last Post: elnk
  rename and add desire "_date" to end of file name before extention RolanRoll 1 1,252 Jun-13-2022, 11:16 AM
Last Post: gruntfutuk
  Unable to upload file using FTP korenron 2 2,496 Dec-23-2021, 12:44 PM
Last Post: korenron
  Rename Files based on XML file klturi421 3 2,218 Oct-22-2021, 07:37 PM
Last Post: klturi421
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 8,175 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  Rename docx file from tuple gjack 2 2,204 Oct-20-2020, 05:33 PM
Last Post: gjack

Forum Jump:

User Panel Messages

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