Am a newbie for python and cherrypy. I am trying to upload file using following code:
#cherrypy.tools.noBodyProcess()
def POST(self,theFile=None):
lcHDRS = {}
for key, val in cherrypy.request.headers.iteritems():
lcHDRS[key.lower()] = val
formFields = myFieldStorage(fp=cherrypy.request.rfile,
headers=lcHDRS,
environ={'REQUEST_METHOD':'POST'},
keep_blank_values=True)
dt = datetime.now()
date = dt.strftime('%Y-%m-%d')
dt = dt.strftime('%Y%m%d%H%M%S')
theFile = formFields['theFile']
theFile.filename = str(dt) + "file"
shutil.copy2(theFile.file.name,os.path.join(absolutePath , theFile.filename))
...
...
I checked the path os.path.join(absolutePath , theFile.filename) and it is coming proper.
The problem is that the code is working fine on Linux-ubuntu, but not on windows.
Error invoked is: Edited
shutil.copy2(theFile.file.name,settings.UPLOAD_FILE_PATH + theFile.filename)
File "C:\Anaconda\lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "C:\Anaconda\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'c:\\users\\username\\appdata\\local\\temp\\tmpjy3gys'
Where am i going wrong?
If you want any other information please let me know.
The issue may be related with some temporary file security which forbids reopening by a filename. Try replace shutil.copy2 call with:
with open('/path/that/you/have/permission/to', 'wb') as f:
shutil.copyfileobj(theFile.file, f)
I guess windows has UAC restrict for launching the program, have you tried run the script under the administrator privilege?
Related
I am attempting to create and write to a temporary file on Windows OS using Python. I have used the Python module tempfile to create a temporary file.
But when I go to write that temporary file I get an error Permission Denied. Am I not allowed to write to temporary files?! Am I doing something wrong? If I want to create and write to a temporary file how should should I do it in Python? I want to create a temporary file in the temp directory for security purposes and not locally (in the dir the .exe is executing).
IOError: [Errno 13] Permission denied: 'c:\\users\\blah~1\\appdata\\local\\temp\\tmpiwz8qw'
temp = tempfile.NamedTemporaryFile().name
f = open(temp, 'w') # error occurs on this line
NamedTemporaryFile actually creates and opens the file for you, there's no need for you to open it again for writing.
In fact, the Python docs state:
Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
That's why you're getting your permission error. What you're probably after is something like:
f = tempfile.NamedTemporaryFile(mode='w') # open file
temp = f.name # get name (if needed)
Use the delete parameter as below:
tmpf = NamedTemporaryFile(delete=False)
But then you need to manually delete the temporary file once you are done with it.
tmpf.close()
os.unlink(tmpf.name)
Reference for bug: https://github.com/bravoserver/bravo/issues/111
regards,
Vidyesh
Consider using os.path.join(tempfile.gettempdir(), os.urandom(24).hex()) instead. It's reliable, cross-platform, and the only caveat is that it doesn't work on FAT partitions.
NamedTemporaryFile has a number of issues, not the least of which is that it can fail to create files because of a permission error, fail to detect the permission error, and then loop millions of times, hanging your program and your filesystem.
The following custom implementation of named temporary file is expanded on the original answer by Erik Aronesty:
import os
import tempfile
class CustomNamedTemporaryFile:
"""
This custom implementation is needed because of the following limitation of tempfile.NamedTemporaryFile:
> Whether the name can be used to open the file a second time, while the named temporary file is still open,
> varies across platforms (it can be so used on Unix; it cannot on Windows NT or later).
"""
def __init__(self, mode='wb', delete=True):
self._mode = mode
self._delete = delete
def __enter__(self):
# Generate a random temporary file name
file_name = os.path.join(tempfile.gettempdir(), os.urandom(24).hex())
# Ensure the file is created
open(file_name, "x").close()
# Open the file in the given mode
self._tempFile = open(file_name, self._mode)
return self._tempFile
def __exit__(self, exc_type, exc_val, exc_tb):
self._tempFile.close()
if self._delete:
os.remove(self._tempFile.name)
This issue might be more complex than many of you think. Anyway this was my solution:
Make use of atexit module
def delete_files(files):
for file in files:
file.close()
os.unlink(file.name)
Make NamedTemporaryFile delete=False
temp_files = []
result_file = NamedTemporaryFile(dir=tmp_path(), suffix=".xlsx", delete=False)
self.temp_files.append(result_file)
Register delete_files as a clean up function
atexit.register(delete_files, temp_files)
tempfile.NamedTemporaryFile() :
It creates and opens a temporary file for you.
f = open(temp, 'w') :
You are again going to open the file which is already open and that's why you are getting Permission Denied error.
If you really wants to open the file again then you first need to close it which will look something like this-
temp= tempfile.NamedTemporaryFile()
temp.close()
f = open(temp.name, 'w')
Permission was denied because the file is Open during line 2 of your code.
close it with f.close() first then you can start writing on your tempfile
I have two almost similar ways to open a file:
first one works fine:
filename = 'RN6531_flat_20ums_3.pr'
pr_file = open(filename,'r')
print(pr_file.readlines())
pr_file = pr_file.close
the second one should do the same
filename = 'RN6531_flat_20ums_3.pr'
with open('filename') as pr_file:
print(pr_file.readlines())
pr_file = pr_file.close
but actually delivers the error message
FileNotFoundError: [Errno 2] No such file or directory: 'filename'
What am I doing wrong? I'm learning coding with python at the moment and don't see the difference. The paths are the same, the file exists, but only one way works as expected.
Fix some typo, also you don't have to close file manually context manager will do it for you
filename = 'RN6531_flat_20ums_3.pr'
with open(filename, 'r') as pr_file:
print(pr_file.readlines())
Is there a way for Python to close that the file is already open file.
Or at the very least display a popup that file is open or a custom written error message popup for permission error.
As to avoid:
PermissionError: [Errno 13] Permission denied: 'C:\\zf.csv'
I've seen a lot of solutions that open a file then close it through python. But in my case. Lets say I left my csv open and then tried to run the job.
How can I make it so it closes the currently opened csv?
I've tried the below variations but none seem to work as they expect that I have already opened the csv at an earlier point through python. I suspect I'm over complicating this.
f = 'C:\\zf.csv'
file.close()
AttributeError: 'str' object has no attribute 'close'
This gives an error as there is no reference to opening of file but simply strings.
Or even..
theFile = open(f)
file_content = theFile.read()
# do whatever you need to do
theFile.close()
As well as:
fileobj=open('C:\\zf.csv',"wb+")
if not fileobj.closed:
print("file is already opened")
How do I close an already open csv?
The only workaround I can think of would be to add a messagebox, though I can't seem to get it to detect the file.
filename = "C:\\zf.csv"
if not os.access(filename, os.W_OK):
print("Write access not permitted on %s" % filename)
messagebox.showinfo("Title", "Close your CSV")
Try using a with context, which will manage the close (__exit__) operation smoothly at the end of the context:
with open(...) as theFile:
file_content = theFile.read()
You can also try to copy the file to a temporary file, and open/close/remove it at will. It requires that you have read access to the original, though.
In this example I have a file "test.txt" that is write-only (chmod 444) and it throws a "Permission denied" error if I try writing to it directly. I copy it to a temporary file that has "777" rights so that I can do what I want with it:
import tempfile, shutil, os
def create_temporary_copy(path):
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, 'temp_file_name')
os.chmod(temp_path, 0o777); # give full access to the tempfile so we can copy
shutil.copy2(path, temp_path) # copy the original into the temp one
os.chmod(temp_path, 0o777); # replace permissions from the original file
return temp_path
path = "./test.txt" # original file
copy_path = create_temporary_copy(path) # temp copy
with open(copy_path, "w") as g: # can do what I want with it
g.write("TEST\n")
f = open("C:/Users/amol/Downloads/result.csv", "r")
print(f.readlines()) #just to check file is open
f.close()
# here you can add above print statement to check if file is closed or not. I am using python 3.5
I have a python script which polls a folder to see if a new file is added, and when a new file is added, it loads the text file into numpy.
before = dict([(f, None) for f in os.listdir('.')])
while 1:
#time.sleep(10)
after = dict([(f, None) for f in os.listdir('.')])
added = [f for f in after if f not in before]
if added:
print added[0]
try:
raw = numpy.loadtxt(added[0])
except IOError:
print "----- Error"
core.wait(0.1)
raw = numpy.loadtxt(added[0])
When I don't try catching the exception, I get an error in reading the file - "Permission denied". I am a Windows administrator, so this should not be an issue. It also only happens on some files, so I think the script might be trying to open the file before it is fully written. Is there any way to get rid of this error?
I want to define a function that takes a file name, and runs the file through some code. I have completed the latter part, but I'm stuck at the first part. Here is where I'm having trouble:
def function(inputfilename):
file = open("inputfilename","r")
example input and the error I get:
>>>function("file.csv")
FileNotFoundError: [Errno 2] No such file or directory: 'inputfilename'
How can I fix this?
You are trying to open a file with inputfilename name.
Replace:
file = open("inputfilename", "r")
with:
file = open(inputfilename, "r")
Also, consider using with context manager while working with files.