I’ve tried to upload a file onto an ftp server by means of a Python program. The file to be uploaded is in c:fichierprog, the python program is in c:fichierprogftp. I’ve tried with 3 situations:
- I first cd to c:fichierprog, and run the program with “python ftppyhonprog.py”, and in the python program the filename contains no directory information. It succeeded.
- I run the program in the directory c:fichierprogftp, but in the program the file to be uploaded contains absolute path. This way I got a lot of error messages.
- I run the program in the directory c:fichierprogftp, but in the program os.chdir(‘c:fichierprog ‘) should change the directory to that one which contains the data file, and the filename does not contain path information as in situation 1. This time no error message, but the file is not uploaded, either.
I cannot explain why 2 and 3 do not work. Can somebody give me advice? Below is the code for situation 3:
from ftplib import FTP
import os
def upload(ftp, file):
cmd = f'STOR {file}'
ftp.storbinary(cmd, open(file, "rb"), 1024)
ftp = FTP()
ftp.connect('...', 21)
ftp.login(...)
ftp.cwd('/TESTIN')
os.chdir('c:\fichier\prog')
print('current dir: ', os.getcwd())
upload(ftp, 'xhzd.tmp')
ftp.rename('xhzd.tmp', 'xhzd.txt')
ftp.close()