I am trying to add FTP functionality into my Python 3 script using only ftplib or other libraries that are included with Python. The script needs to delete a directory from an FTP server in order to remove an active web page from our website.
The problem is that I cannot find a way to delete the .htaccess file using ftplib and I can't delete the directory because it is not empty.
Some people have said that this is a hidden file, and have explained how to list hidden files, but I need to delete the file, not list it. My .htaccess file also has full permissions and it can be successfully deleted using most other FTP clients.
Sample code:
files = list(ftp.nlst(myDirectory))
for f in files:
ftp.delete(f)
ftp.rmd(myDirectory)
Update: I was able to get everything working correctly, here is the complete code:
ftp.cwd(myDirectory) # move to the dir to be deleted
#upload placeholder .htaccess in case there is none in the dir and then delete it
files01 = "c:\\files\\.htaccess"
with open(files01, 'rb') as f:
ftp.storlines('STOR %s' % '.htaccess', f)
ftp.delete(".htaccess")
print("Successfully deleted .htaccess file in " + myDirectory)
files = list(ftp.nlst(myDirectory)) # delete files in dir
for f in files:
ftp.delete(f)
print("Successfully deleted visible files in " + myDirectory)
ftp.rmd(myDirectory) # remote directory deletion
print("Successfully deleted the following directory: " + myDirectory)
Related
My flask app has a function that compresses log files in a directory into a zip file and then sends the file to the user to download. The compression works, except that when the client receives the zipfile, the zip contains a series of folders that matches the absolute path of the original files that were zipped in the server. However, the zipfile that was made in the server static folder does not.
Zipfile contents in static folder: "log1.bin, log2.bin"
Zipfile contents that was sent to user: "/home/user/server/data/log1.bin, /home/user/server/data/log2.bin"
I don't understand why using "send_file" seems to make this change to the zip file contents and fills the received zip file with sub folders. The actual contents of the received zip file do in fact match the contents of the sent zip file in terms of data, but the user has to click through several directories to get to the files. What am I doing wrong?
#app.route("/download")
def download():
os.chdir(data_dir)
if(os.path.isfile("logs.zip")):
os.remove("logs.zip")
log_dir = os.listdir('.')
log_zip = zipfile.ZipFile('logs.zip', 'w')
for log in log_dir:
log_zip.write(log)
log_zip.close()
return send_file("logs.zip", as_attachment=True)
Using send_from_directory(directory, "logs.zip", as_attachment=True) fixed everything. It appears this call is better for serving up static files.
Using python program, I was able to download multiple source files from a FTP server (using ftplib and os libraries) to my local machine.
These source file resides at a particular directory inside the FTP server.
I was able to download the source files only if I have provided the same directory path in my local machine, as of FTP directory path.
I am able to download the files into C:\data\abc\transfer which is same as remote directory /data/abc/transfer. Code is insisting me to provide the same directory.
But I want to download all files into my desired directory C:\data_download\
Below is the code :
import ftplib
import os
from ftplib import FTP
Ftp_Server_host = 'xcfgn#wer.com'
Ftp_username ='qsdfg12'
Ftp_password = 'xxxxx'
Ftp_source_files_path = '/data/abc/transfer/'
ftp = FTP(Ftp_Server_host)
ftp.login(user=Ftp_username, passwd=Ftp_password)
local_path = 'C:\\data_download\\'
print("connected to remote server :" + Ftp_Server_host)
print()
ftp_clnt = ftp_ssh.open_sftp()
ftp_clnt.chdir(Ftp_source_files_path)
print("current directory of source file in remote server :" +ftp_clnt.getcwd())
print()
files_list = ftp.nlst(Ftp_source_files_path)
for file in files_list:
print("local_path :" + local_path)
local_fn = os.path.join(local_path)
print(local_fn)
print('Downloading files from remote server :' + file)
local_file = open (local_fn, "wb")
ftp.retrbinary("RETR " + file, local_file.write)
local_file.close()
print()
print("respective files got downloaded")
print()
ftp_clnt.close()
You have to provide a full path to open function, not just a directory name.
To assemble a full local path, take a file name from the remote paths returned by ftp.nlst and combine them with the target local directory path.
Like this:
local_fn = os.path.join(local_path, os.path.basename(file))
I need to upload an image file to a certain folder. It works fine on localhost but if I push the app to heroku it tells me:
IOError: [Errno 2] No such file or directory: 'static/userimg/1-bild-1.jpg'
Which means it cant find the folder?
I need to store the image files there for a few seconds to perform some actions on theme. After that they will be sent to AWS and will be deleted from the folder.
Thats the code where I save the images into the folder:
i = 1
for key, file in request.files.iteritems():
if file:
filename = secure_filename(str(current_user.id) + "-bild-"+ str(i) +".jpg")
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
i = i + 1
Later I get the files from the folder like this:
for item in os.listdir(os.path.join(app.config['UPLOAD_FOLDER'])):
if item.startswith(str(current_user.id)) and item.split(".")[0].endswith("1"):
with open(os.path.join(app.config['UPLOAD_FOLDER'], item), "rb") as thefile:
data = base64.b64encode(thefile.read())
upload_image_to_aws_from_image_v3('MYBUCKET', "userimg/", data, new_zimmer, "hauptbild", new_zimmer.stadt, new_zimmer.id)
os.remove(str(thefile.name))
That is the upload folder in app config:
UPLOAD_FOLDER = "static/userimg/"
Everything works fine on localhost.
I had to add the absolute path to the directory, because the path on the server is different.
You can run heroku run bash for your app through the CLI and check the directories with dir. Use then cd .. to go back or cd *directory name* to go into the directory.
I had to add
MYDIR = os.path.dirname(__file__)
and replace all
os.path.join(app.config['UPLOAD_FOLDER']
with
os.path.join(MYDIR + "/" + app.config['UPLOAD_FOLDER']
Some informations on the topic also here:
Similar question
right now this is all I have:
import ftputil
a_host = ftputil.FTPHost("ftp_host", "username","pass") # login to ftp
for (dirname, subdirs, files) in a_host.walk("/"): # directory
for f in files:
fullpath = a_host.path.join(dirname, f)
if fullpath.endswith('html'):
#stucked
so I can log in to my ftp, and do a .walk in my files
the thing I am not able to manage is when the .walk finds a html file to also search in it for a string I want.
for example:
on my ftp - there is a index.html and a something.txt file
I want to find with .walk the index.html file, and then in index.html search for 'my string'
thanks
FTP is a protocol for file transfer only. It has not the ability by itself to execute remote commands which are needed to search the files on the remote server (there is a SITE command but it can usually not be used for such a purpose because it is not implemented or restricted to only a few commands).
This means your only option with FTP is to download the file and search it locally, i.e. transfer the file to the local system, open it there and look for the string.
I've been trying to download files from an FTP server. For this I've found this Python-FTP download all files in directory and examined it. Anyways, I extracted the code I needed and it shows as follows:
import os
from ftplib import FTP
ftp = FTP("ftp.example.com", "exampleUsername", "examplePWD")
file_names = ftp.nlst("\public_html")
print file_names
for filename in file_names:
if os.path.splitext(filename)[1] != "":
local_filename = os.path.join(os.getcwd(), "Download", filename)
local_file = open(filename, 'wb')
ftp.retrbinary('RETR ' + filename, local_file.write)
local_file.close()
ftp.close()
But when it tries to open the file, it keeps saying:
ftplib.error_perm: 550 Can't open CHANGELOG.php: No such file or directory
I've tried w+, a+, rw, etc. and I keep getting the same error all the time. Any ideas?
Note: I am using OSX Mavericks and Python 2.7.5.
This question may have been asked several times and believe me I researched and found some of them and none of them worked for me.
open() in Python does not create a file if it doesn't exist
ftplib file select
It looks like you are listing files in a directory and then getting files based on the returned strings. Does nlst() return full paths or just filenames? If its just filenames than retrbinary might be expecting "/Foo/file" but getting "file", and there might not be anything named file in the root dir of the server.