moving files to new folder in FTP using Python - python

I am a bit lost. I'm trying to move a bunch of files to a new folder in FTP using python. I have tried a lot of function but what seems to work best is the ftp.rename function. In fact, it works to move only one file at a time to a new folder but it doesn't work to do it for a lot of files (like in my screenshot) using a for loop.
Do you know another technique to move efficiently files in a new folder?
Please help
This is the code to move a single file :
ftp = ftplib.FTP(host, username, password)
ftp.encoding = "utf-8"
FtpImage = ftp.mkd("image")
ftp.rename("img1.png", "/image/img1.png")
ftp.quit()
This is the code to move a bunch of files at the same time :
ftp = ftplib.FTP(host, username, password)
ftp.encoding = "utf-8"
#creating a list with all my files
dirList = ftp.nlst()
#creating a folder
ftpFolder = ftp.mkd("folder1")
#moving my file using their name and adding the folder1 to their name
for file in dirList:
ftp.rename(file, "/folder1/" + file)
# shutil.move(file, "/folder1/" + file )
ftp.quit()
Error that I get when I run the second programm:
DeprecationWarning: The Tix Tk extension is unmaintained, and the tkinter.tix wrapper module is deprecated in favor of tkinter.ttk
from tkinter.tix import IMAGETEXT
Traceback (most recent call last):
File "\\wsl$\Ubuntu\home\q******\projet_python\FTP-sorting\test.py", line 26, in <module>
ftp.rename(file, "/folder1/")
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 604, in rename
return self.voidcmd('RNTO ' + toname)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 286, in voidcmd
return self.voidresp()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 259, in voidresp
resp = self.getresp()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 254, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 Rename /folder1/: Device or resource busy

I was able to find a way to sort my different files :
I had first to sort my dirList (list with all the files) with new sub list (like allDivers) and then I used the following code
for file in allDivers:
destination_folder = "/divers/"
destination = destination_folder + file
ftp.rename(file, destination )

Related

Why can't i create a folder when he doesn't exist?

i 'm trying to make a for loop who browse files in a specific directory while creating a folder if he doesn't exist with this solution. here is the code:
import ftputil
host=ftputil.FTPHost('x.x.x.x',"x","x") #connecting to the ftp server
mypathexist='./CameraOld' (he is here: /opt/Camera/CameraOld
mypath = '.' #it put you in /opt/Camera (it's the default path configured)
host.chdir(mypath)
files = host.listdir(host.curdir)
for f in files: #i browse the files in my folders
if f==mypathexist: #if a file is named CameraOld (it's a folder)
isExist=True
break
else: isExist=False #if 0 file are named like it
print(isExist)
if isExist==False: #if the file doesn't exist
host.mkdir(mypathexist) #create the folder
else:
print("ok")
The problem is that isExist is always false so the script try to create a folder who is already created. And i don't understand why.
Here's the output:
False #it's the print(isExist)
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 695, in command
self._session.mkd(path)
File "/usr/lib/python3.10/ftplib.py", line 637, in mkd
resp = self.voidcmd('MKD ' + dirname)
File "/usr/lib/python3.10/ftplib.py", line 286, in voidcmd
return self.voidresp()
File "/usr/lib/python3.10/ftplib.py", line 259, in voidresp
resp = self.getresp()
File "/usr/lib/python3.10/ftplib.py", line 254, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 CameraOld: file exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/user/Bureau/try.py", line 16, in <module>
host.mkdir(mypathexist)
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 697, in mkdir
self._robust_ftp_command(command, path)
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 656, in _robust_ftp_command
return command(self, tail)
File "/usr/local/lib/python3.10/dist-packages/ftputil/host.py", line 694, in command
with ftputil.error.ftplib_error_to_ftp_os_error:
File "/usr/local/lib/python3.10/dist-packages/ftputil/error.py", line 195, in __exit__
raise PermanentError(
ftputil.error.PermanentError: 550 CameraOld: file exist
Debugging info: ftputil 5.0.4, Python 3.10.4 (linux)
I would bet your mypathexist is not correct. Or the other way around, your file list, doesn't hold the strings in that condition you assume it does.
Take a look at your condition by hand. Print out f in your loop. Is it what you would expect to be?
In the end, Python is simply comparing Strings.

Extract a .TAR file within a .gz file

I have to untar around fifty *.gz files in a directory. Inside each *.gz file there is a *.TAR file and some other files.
I am trying a python script which extracts the contents of the *.gz files to a directory. But, I am not able to extract the *.TAR files inside the same directory to which the contents of *.gz are extracted.
This is how the script looks:
import tarfile
import os
import glob
basedir = "path_to _dir"
for i in glob.glob(basedir +"*.gz"):
a = os.path.basename(i)
b = os.path.splitext(a)[0]
c = os.path.splitext(b)[0]
os.mkdir(os.path.join(basedir,c))
t1 = tarfile.open(i)
t1.extractall(c)
for j in os.listdir(c):
if j.endswith('.TAR'):
print(j)
t2 = tarfile.open(j)
t2.extractall()
t2.close()
t1.close()
Its giving me the error:
Traceback (most recent call last):
File "./untar.py", line 16, in <module>
t2 = tarfile.open(j)
File "/usr/lib64/python2.7/tarfile.py", line 1660, in open
return func(name, "r", fileobj, **kwargs)
File "/usr/lib64/python2.7/tarfile.py", line 1722, in gzopen
fileobj = bltn_open(name, mode + "b")
IOError: [Errno 2] No such file or directory: '0299_0108060501.TAR'
0299_0108060501.TAR is the file contained inside the *.gz file
It seems to me that I am doing something very wrong fundamentally, but I don't know what.
Since tar.gz files are TAR archives compressed with gzip one should use
t1 = tarfile.open(i, 'r:gz')
as per documentation.
Also, you need to combine the path of the inner file with the directory being inspected, like so:
t2 = tarfile.open(os.path.join(c, j))

Unzipping file results in "BadZipFile"

I know there are similar questions but neither of them provided a solution for my problem. I am using the following code:
import os, glob
import zipfile
root = 'E:\\xx\\fashion\\*'
directory = 'E:\\xx\\fashion\\'
extension = ".zip"
date_file_list = []
for folder in glob.glob(root):
if folder.endswith(extension): # check for ".zip" extension
print(folder)
zipfile.ZipFile(os.path.join(directory, folder)).extractall(os.path.join(directory, os.path.splitext(folder)[0]))
os.remove(folder) # delete zipped file_name
And I get the following error:
Traceback (most recent call last):
File "C:/Users/xx/unzip.py", line 12, in <module>
zipfile.ZipFile(os.path.join(directory, folder)).extractall(os.path.join(directory, os.path.splitext(folder)[0]))
File "C:\Users\xx\AppData\Local\Programs\Python\Python35\lib\zipfile.py", line 1026, in __init__
self._RealGetContents()
File "C:\Users\xx\AppData\Local\Programs\Python\Python35\lib\zipfile.py", line 1094, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file
Some of the files are compressed in winzip some of them are in 7zip. But there are too many files to unzip.
Anybody know why this error is occurring?

python 3: permission for ftp command "dir" in certain directories denied

The situation:
I'm trying to code a ftp file auto up and download program. It will iterate through my files and auto upload new files. The ftp server is build into my wifi router. I have included the ftp-server into my Ubuntu filesystem and everything work's fine (I can access everything). The home site is like family folders and my own folder.
Now my problem:
So far I was able to connect to the server and change into my directory and list the folders. But if I want to change from this directory into e.g. C++ (it's a folder inside my directory), I'll get an error message saying "Permission denied". The user account used to login is the same used in my file-system inclusion.
The python 3 code:
from ftplib import FTP_TLS
class ftp_client():
def __init__(self, host, unandpw):
self.host = host
self.logindata = unandpw #should be a touple
self.ftp = FTP_TLS(self.host)
self.ftp.login(self.logindata[0], self.logindata[1])
self.ftp.prot_p()
def scan(self):
self.ftp.cwd("/" + self.logindata[0] + "/")
self.listoffiles = self.ftp.nlst()
print("list of files: " + str(self.listoffiles))
for file in self.listoffiles:
self.ftp.cwd("/" + str(file) + "/")
self.listoffilesinfiles = self.ftp.nlst()
print(self.listoffilesinfiles)
self.ftp.cwd("/" + self.logindata[0] + "/")
I'm calling it via an external main file...
The console output:
list of files: ['C++', 'Documents', 'Pictures', 'Python', 'School',
'share', 'Videos']
Traceback (most recent call last):
File "main.py", line 5, in <module>
ftp.scan()
File "/home/simon/Python/autoserversync/client.py", line 18, in scan
self.ftp.cwd("/" + str(file) + "/")
File "/usr/lib/python3.5/ftplib.py", line 630, in cwd
return self.voidcmd(cmd)
File "/usr/lib/python3.5/ftplib.py", line 277, in voidcmd
return self.voidresp()
File "/usr/lib/python3.5/ftplib.py", line 250, in voidresp
resp = self.getresp()
File "/usr/lib/python3.5/ftplib.py", line 245, in getresp
raise error_perm(resp)
ftplib.error_perm: 550 /C++/: Permission denied.
Now my question (obviously) is: how can I fix this?
In your self.ftp.cwd("/" + str(file) + "/") line, you're adding a slash to the start of your path. Remember that in Unix, a path starting with a slash is an absolute path, starting from the root directory.
If your folders are in your home directory instead (judging by their names I'm guessing they are) try changing it to self.ftp.cwd("~/" + str(file) + "/") instead.

Python Zipfile - Invalid Argument Errno 22

I have a .zip file and would like to know the names of the files within it. Here's the code:
zip_path = glob.glob(path + '/*.zip')[0]
file = open(zip_path, 'r') # opens without error
if zipfile.is_zipfile(file):
print str(file) # prints to console
my_zipfile = zipfile.ZipFile(zip_path) # throws IOError
Here is the traceback:
<open file u'/Users/me/Documents/project/uploads/assets/peter/offline_message/offline_imgs.zip', mode 'r' at 0x107b2a150>
Traceback (most recent call last):
File "/Users/me/Documents/project/admin_dev/proj_name/views.py", line 1680, in get_dps_app_builder_assets
link_to_assets_zip = zip_dps_app_builder_assets(server_url, app_slug, button_slugs)
File "/Users/me/Documents/project/admin_dev/proj_name/views.py", line 1724, in zip_dps_app_builder_assets
my_zipfile = zipfile.ZipFile(zip_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 712, in __init__
self._GetContents()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 746, in _GetContents
self._RealGetContents()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 779, in _RealGetContents
fp.seek(self.start_dir, 0)
IOError: [Errno 22] Invalid argument
I am very confused as to why this is happening since the file is clearly there and is a valid .zip file. The documentation clearly states that you can pass it either the path to the file or a file-like object, neither of which work in my case:
http://docs.python.org/2/library/zipfile#zipfile-objects
I was not able to figure this issue out and ended up doing it a different way entirely.
EDIT: In the Django app I work with, users needed to be able to upload assets in the form of .zip files and later download everything they had uploaded (plus other content we generate dynamically) in another zip with a different structure. So, I wanted to unzip a previously uploaded file and zip up the contents of that file up in another zip, which I couldn't do because of the error. Instead of reading the zip file when the user requested the download, I ended up unzipping it from a Django InMemoryUploadedFile (whose contents I was able to successfully read) and just leaving the unzipped files on the file system to work with later. The contents of the zip are only two smallish image files, so this workaround of unzipping the zip ahead of time to be used later worked OK for my purposes.

Categories

Resources