The code below 1. Identifies files that are created in a directory and 2. Uploads them to my webserver.
My problem is that the program is only successful when I copy and paste a file into the directory "path_to_watch". The program fails when I use a third-party program (NBA Live 06) that creates files in the "path_to_watch" directory.
The error I receive is: "PermissionError: [Errno 13] Permission denied: 'filename.txt'"
import os, time
from ftplib import FTP
def idFiles():
path_to_watch = r"c:\Users\User\gamestats"
before = dict ([(f, None) for f in os.listdir (path_to_watch)])
while True:
time.sleep (1)
after = dict ([(f, None) for f in os.listdir (path_to_watch)])
added = [f for f in after if not f in before]
if added:
## edit filename to prepare for upload
upload = str(", ".join (added))
ftp = FTP('www.website.com')
ftp.login(user='username', passwd='password')
ftp.cwd('archives')
## error is called on this following line
ftp.storbinary('STOR ' + upload, open(upload, 'rb'))
#resets timer
before = after
idFiles()
Many thanks in advance for any help.
If the third party program has opened the files in an exclusive mode (which is the default) then you cannot open them yourself until it has let go of them.
Considering that it's third party code you can't change the mode in which the file is open, but you'll have to wait for the program to close the files before trying to manipulate them.
See also this question
Related
My code takes a list of file and folder paths, loops through them, then uploads to a Google Drive. If the path given is a directory, the code creates a .zip file before uploading. Once the upload is complete, I need the code to delete the .zip file that was created but the deletion throws an error: PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Temp\Newfolder.zip'. The file_path being given is C:\Temp\Newfolder. From what I can tell the only process using the file is this script but with open(...) should be closing the file when the processing is complete. Looking for suggestions on what could be done differently.
import os
import zipfile
import time
def add_list(filePathList, filePath):
filePathList.append(filePath)
return filePathList
def deleteFiles(filePaths):
for path in filePaths:
os.remove(path)
for file_path in file_paths:
if os.path.isdir(file_path) and not os.path.splitext(file_path)[1]:
# Compress the folder into a .zip file
folder_name = os.path.basename(file_path)
zip_file_path = os.path.join('C:\\Temp', folder_name + '.zip')
with zipfile.ZipFile(zip_file_path, 'w') as zip_file:
for root, dirs, files in os.walk(file_path):
for filename in files:
file_to_zip = os.path.join(root, filename)
zip_file.write(file_to_zip, os.path.relpath(file_to_zip, file_path))
zip_file.close()
# Update the file path to the zipped file
file_path = zip_file_path
# Create request body
request_body = {
'name': os.path.basename(file_path),
'mimeType': 'application/zip' if file_path.endswith('.zip') else 'text/plain',
'parents': [parent_id],
'supportsAllDrives': True
}
#Open the file and execute the request
with open(file_path, "rb") as f:
media_file = MediaFileUpload(file_path, mimetype='application/zip' if file_path.endswith('.zip') else 'text/plain')
upload_file = service.files().create(body=request_body, media_body=media_file, supportsAllDrives=True).execute()
# Print the response
print(upload_file)
if file_path.endswith('.zip'):
add_list(filePathList, file_path)
time.sleep(10)
deleteFiles(filePathList)
You're using with statements to properly close the open files, but you're not actually using the open files, just passing the path to another API that is presumably opening the file for you under the hood. Check the documentation for the APIs here:
media_file = MediaFileUpload(file_path, mimetype='application/zip' if file_path.endswith('.zip') else 'text/plain')
upload_file = service.files().create(body=request_body, media_body=media_file, supportsAllDrives=True).execute()
to figure out if MediaFileUpload objects, or the various things done with .create/.execute that uses it, provides some mechanism for ensuring deterministic resource cleanup (as is, if MediaFileUpload opens the file, and nothing inside .create/.execute explicitly closes it, at least one such file is guaranteed to be open when you try to remove them at the end, possibly more than one if reference cycles are involved or you're on an alternate Python interpreter, causing the problem you see on Windows systems). I can't say what might or might not be required, because you don't show the implementation, or specify the package it comes from.
Even if you are careful not to hold open handles yourself, there are cases where other processes can lock it, and you need to retry a few times (virus scanners can cause this issue by attempting to scan the file as you're deleting it; I believe properly implemented there'd be no problem, but they're software written by humans, and therefore some of them will misbehave). It's particularly common for files that are freshly created (virus scanners being innately suspicious of any new file, especially compressed files), and as it happens, you're creating fresh zip files and deleting them in short order, so you may be stuck retrying a few times.
I am trying to make a keylogger, it works on my computer but when i turn it into an executable it gives me an error "Python failed to execute script keylogger" I think its a path problem because its static and every computer have a diffrent directory, i am a beginner i could use some help.
files = ["log.txt", "info.txt", "clipboard.txt", "screenshot.png"]
dir_path=r"C:/Users/messa/Desktop/Python keylogger/"
##This function to take a screenshot---------------
def takess():
im = ImageGrab.grab()
im.save(dir_path+ "/" + "screenshot.png")
## i have multiple functions tell me if this is not enough .
My solution idea: I tried to check for this path if it does not exist i'll create it, after creating the directory i want to create the files in this directory, but it gives me a permission error " PermissionError: [Errno 13] Permission denied:"
extend = "\\"
dir_path="C:\\Users\\Default\\AppData\\Local"
Path(dir_path).mkdir(parents=True, exist_ok=True)
files = ["log.txt", "info.txt", "clipboard.txt", "screenshot.png"]
for file in files:
f= open(dir_path + extend + file, "w+")
f.close()
You can use pathlib module to get a stored location
import pathlib
dir_path = pathlib.Path().absolute()
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)
I am supposed to compress this directory called "log" which contains various logs(almost 50 logs) from different devices. This is the code so far I have tried...
import tarfile
import os
import glob
def value():
tar = tarfile.open("Archive.tgz","w:gz")
path = '/var/log'
cwd = os.getcwd()
for name in glob.glob(path):
tar.add(name, arcname = "log")
tar.close()
if os.path.isfile('./Archive.tgz'):
print "Compressed Successfully !"
else:
print "File not Found!"
value()
I keep getting this error though:
File "tar2.py", line 18, in <module>
value()
File "tar2.py", line 11, in value
tar.add(name, arcname = "log")
File "../../../../../../../src/dist/python/Lib/tarfile.py", line 2002, in add
File "../../../../../../../src/dist/python/Lib/tarfile.py", line 1994, in add
IOError: [Errno 13] Permission denied: '/var/log/cscript.log'
I read a lot about this error online and tried using chmod as well. But nothing works.
Please help me out!
This error (EACCES) is propagated from the underlying OS (filesystem) and there isn't much you could do about it with your script. You need to either give (read) permission to the user running your script. Or you can run your script with another user that has sufficient rights on that file.
You mention having changed mode on the file, but could it be that access was still not granted to the effective user id under which the script runs? You can verify your current user id and groups it is member of by running id in shell. And for instance long listing of the file will tell you current mode setting (ls -l /var/log/cscript.log).
It can also happen. Even if you change the permission bits, the process writing this file can check and reset the, Or the file gets created anew with original mode/ownership information. And the process writing into it has been restarted.
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.