I have a zip file to upload. I know how to upload it.I open the file with "Rb" mode. When i want to extract the zip file that i uploaded i get an error and files in the ZIP archive are gone, i think that's because of the "Rb" mode . I don't know how to extract my uploaded file.
Here is the code:
filename="test.zip"
ftp=ftplib.FTP("ftp.test.com")
ftp.login('xxxx','xxxxx')
ftp.cwd("public_html/xxx")
myfile=open("filepath","rb")
ftp.storlines('STOR ' + filename,myfile)
ftp.quit()
ftp.close()
Your code is currently using ftp.storlines() which is intended for use with ASCII files.
For binary files such as ZIP files, you need to use ftp.storbinary() instead:
import ftplib
filename = "test.zip"
with open(filename, 'rb') as f_upload:
ftp = ftplib.FTP("ftp.test.com")
ftp.login('xxxx', 'xxxxx')
ftp.cwd("public_html/xxx")
ftp.storbinary('STOR ' + filename, f_upload)
ftp.quit()
ftp.close()
When ASCII mode is used on a ZIP file, it will result in an unusable file which is what you were getting.
Related
So I'm currently working on a project where I should be able to select a folder, encrypt everything inside that folder and put it into a .zip file.
The code where I encrypt the files and put them into the .zip file is this one:
with zipfile.ZipFile(path,'w') as my_zip3:
for folderName, subfolders, filenames in os.walk(directoryname):
for filename in filenames:
print(filename)
self.encrypt(filename,key)
my_zip3.write(os.path.join(folderName, filename))
Now, the problem is that when I ONLY use the "print(filename)" part, it prints all the files correctly, but when I add the other 2 lines of code to encrypt and add the files to the zip, it just gives me this error: "FileNotFoundError: [Errno 2] No such file or directory: file"
I also have other parts of the code that WORK where I do the same without the encrypting part.
Here's the encrypt function:
def encrypt(self, filename, key):
f = Fernet(key)
with open(filename, "rb") as file:
file_data = file.read()
encrypted_data = f.encrypt(file_data)
with open(filename, "wb") as file:
file.write(encrypted_data)
I assume that the error gets thrown in the line with open(filename, "rb") as file: within the encryption.
With filename, you only get the name of the file, not the path to it. Since you're walking through a directory, you could be anywhere inside that structure, so the is not found.
Try concatinating the directory path as you did with os.path.join(folderName, filename).
# Unzip the dataset (if we haven't already)
if not os.path.exists('./cola_public/'):
!unzip cola_public_1.1.zip
The above code will unzip a file in jupyter notebook.
How would I do this in a similar fashion if the file was a .gz file?
The zipfile package works pretty well for gzip
import zipfile as zf
file = zf.ZipFile("/path/to/file/YOUR_FILE.gzip")
I assume that your file was tar.gz and it contains more files, then you can use. (You need to create test folder or use root)
with tarfile.open('TEST.tar.gz', 'r:gz') as _tar:
for member in _tar:
if member.isdir():#here write your own code to make folders
continue
fname = member.name.rsplit('/',1)[1]
_tar.makefile(member, 'TEST' + '/' + fname)
Or if your gz is not a tar file and contains a single file you can use gzip
Reference:- https://docs.python.org/2/library/gzip.html#examples-of-usage
import gzip
import shutil
def gunzip(file_path,output_path):
with gzip.open(file_path,"rb") as f_in, open(output_path,"wb") as f_out:
shutil.copyfileobj(f_in, f_out)
f_in.close()
f_out.close()
f='TEST.txt.gz'
gunzip(f,f.replace(".gz",""))
i'm trying to do a program here i need to compress some files, but i want it to stop when the file doesn't exist.
The code works, but the thing is that it compresses the file anyway, what i mean is that the program outputs the error but compress a file with that name (an empty file)
if someone could help it would be wonderful :)
import sys, zipfile
def compress (file):
try:
zf = zipfile.ZipFile(file + '.zip', mode='w')
zf.write(file, compress_type=zipfile.ZIP_DEFLATED)
zf.close()
except OSError:
print("The file "+ file + " doesnt exist!")
#erro.value = 1
if __name__ == "__main__":
compress(sys.argv[1])
From Python documentation:
If the file is created with mode 'w', 'x' or 'a' and then closed without adding any files to the archive, the appropriate ZIP structures for an empty archive will be written to the file.
So use
if os.path.exists(file)
to check if the file exists, before
zf = zipfile.ZipFile(file + '.zip', mode='w')
I'm trying to upload a file using ftp in python, but I get an error saying:
ftplib.error_perm: 550 Filename invalid
when I run the following code:
ftp = FTP('xxx.xxx.x.xxx', 'MY_FTP', '')
ftp.cwd("/incoming")
file = open('c:\Automation\FTP_Files\MaxErrors1.json', 'rb')
ftp.storbinary('STOR c:\Automation\FTP_Files\MaxErrors1.json', file)
ftp.close()
I've checked that the file exists in the location I specified, does anyone know what might be causing the issue?
The problem is that on the server, the path c:\Automation\FTP_Files\MaxErrors1.json is not valid. Instead try just doing:
ftp.storbinary('STOR MaxErrors1.json', file)
The argument to STOR needs to be the destination file name, not the source path. You should just do ftp.storbinary('STOR MaxErrors1.json', file).
you should upload file without absolute path in ftp server
for example :
import ftplib
session = ftplib.FTP('server.address.com','USERNAME','PASSWORD')
file = open('kitten.jpg','rb') # file to send
session.storbinary('STOR kitten.jpg', file) # send the file
file.close() # close file and FTP
session.quit()
I have to copy a ziped folder using ftplib as follows:
ftp = FTP('ip')
ftp.login(user='user', passwd = 'pass')
filename= "D:/sample.zip"
ftp.storlines("STOR " + os.path.basename(filename), open(filename,"r"))
On the remote the sample folder does gets copied but it is just '1kb' size in actual its size is 2963Kb. So, could you help me out how shall i copy the complete ziped folder on the remote.
Firstly, use storbinary() and not storlines. The latter is for ASCII files.
And since zip files are binary, the file should be opened in binary mode:
ftp.storbinary("STOR " + os.path.basename(filename), open(filename, "rb"))