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?
Related
I'm using this to connect to Azure File Share and upload a file. I would like to chose what extension file will have, but I can't. I got an error shown below. If I remove .txt everything works fine. Is there a way to specify file extension while uploading it?
Error:
Exception: ResourceNotFoundError: The specified parent path does not exist.
Code:
def main(blobin: func.InputStream):
file_client = ShareFileClient.from_connection_string(conn_str="<con_string>",
share_name="data-storage",
file_path="outgoing/file.txt")
f = open('/home/temp.txt', 'w+')
f.write(blobin.read().decode('utf-8'))
f.close()
# Operation on file here
f = open('/home/temp.txt', 'rb')
string_to_upload = f.read()
f.close()
file_client.upload_file(string_to_upload)
I believe the reason you're getting this error is because outgoing folder doesn't exist in your file service share. I took your code and ran it with and without extension and in both situation I got the same error.
Then I created a folder and tried to upload the file and I was able to successfully do so.
Here's the final code I used:
from azure.storage.fileshare import ShareFileClient, ShareDirectoryClient
conn_string = "DefaultEndpointsProtocol=https;AccountName=myaccountname;AccountKey=myaccountkey;EndpointSuffix=core.windows.net"
share_directory_client = ShareDirectoryClient.from_connection_string(conn_str=conn_string,
share_name="data-storage",
directory_path="outgoing")
file_client = ShareFileClient.from_connection_string(conn_str=conn_string,
share_name="data-storage",
file_path="outgoing/file.txt")
# Create folder first.
# This operation will fail if the directory already exists.
print "creating directory..."
share_directory_client.create_directory()
print "directory created successfully..."
# Operation on file here
f = open('D:\\temp\\test.txt', 'rb')
string_to_upload = f.read()
f.close()
#Upload file
print "uploading file..."
file_client.upload_file(string_to_upload)
print "file uploaded successfully..."
I am getting a permission's error when writing multiple files to a specific directory from a url, specifically an SFTP site. Here is an example of what I am trying to run.
import pycurl, urllib2, requests, json, pprint, urllib, os
from io import BytesIO
files = []
c = pycurl.Curl()
data = BytesIO()
c.setopt(c.URL, "https://sftp.mmm.com:443/api/v1.1/files/Source_Directory/")
c.setopt(c.USERPWD, "username:password")
c.setopt(c.WRITEDATA, data)
c.perform()
c.close()
dictionary = json.loads(data.getvalue())
for i in dictionary['files']:
files.append(i["fileName"])
for x in files:
c = pycurl.Curl()
file = open(x, "a+")
c.setopt(c.URL, "https://sftp.pjm.com:443/api/v1.1/files/Source_directory/"+x)
c.setopt(c.USERPWD, "username:password")
c.setopt(c.WRITEDATA, file)
c.perform()
c.close()
file.close()
When i use "x" in the file - open (x, "a+") the files write to my python project folder. However if I replace the x with a specific directory like my desktop I get IOError: [Errno 13] Permission denied: "Destination_Directory"
This is most likely because what you are doing requires administrator. Simply run your IDE as administrator (by right clicking and clicking run as administrator). If you are running it from command prompt, run command prompt as administrator.
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 function that polls a folder for new files, then loads them using numpy.loadtext when it shows up. The function is called from a while loop that runs for 30 seconds. The function works properly most of the time, but for some files, seemingly at random, I get the error IOError: [Errno 13] Permission denied: 'myfilename1.txt'. Here is the content of my function:
before = dict([(f, None) for f in os.listdir(mydir)])
while 1:
after = dict([(f, None) for f in os.listdir(mydir)])
added = [f for f in after if f not in before]
# New File
if added:
raw = numpy.loadtxt(mydir + added[0])
return raw
Any idea on why this is happening? It properly polls and reads most text files that are incoming, but sometimes spits the error and I can't find a systematic reason why.
UPDATE:
Has something to do with using the full path with loadtxt. When I change the working directory to the directory where the files are, I no longer get the permissions error.
Have you tried opening the file as read only, may be a conflict if the file is accessed by another application (or is still currently being created).
# New File
if added:
with open(mydir + added[0], 'r') as f:
raw = numpy.loadtxt(f)
You could also try some form of IOError handling which waits a little while and then tries again
import time
before = dict([(f, None) for f in os.listdir(mydir)])
added = False
while 1:
# New File
if added:
try:
raw = numpy.loadtxt(mydir + added[0])
return raw
except IOError:
time.sleep(5)
else:
after = dict([(f, None) for f in os.listdir(mydir)])
added = [f for f in after if f not in before]
I got the same error when I attempted the following:
Y = np.loadtxt("C:/Users/erios/images_3_color_15k_labeled/", dtype='int')
I.e., I passed the folder where the text was located
INstead, the following command executed with no error:
Y = np.loadtxt("C:/Users/erios/images_3_color_15k_labeled/labels_for_locations.txt", dtype='int')
In sum, specify the full name of the text file, not just the folder.
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?