I am trying to write a datafame to a csv file using the following:
fxRollPath = 'C:\Users\stacey\Documents\scripts\FXFiles'
fxData.to_csv(fxRollPath,fxRoll'+ str(index)+'.csv')
Where fxRoll is a dataframe
But I get the error:
PermissionError: [Errno 13] Permission denied: 'C:\Users\stacey\Documents\scripts\FXFiles'
What have I done wrong?
Just realized, you are actually trying to save to a target directory path instead of file path.
Docs of path_or_buf for DataFrame.to_csv : "string or file handle, default None. File path or object, if None is provided the result is returned as a string."
So change your code to:
fxData.to_csv('{0}\{1}{2}{3}'.format(fxRollPath, fxRoll, str(index), '.csv'))
Maybe you need close the first version your csv which now is open
I got this problem too. Not sure if you have the same situation as me.
I had the same filename in the same directory and I wanted to override the old csv file. Instead of overriding the old file, I deleted it and then save it to solve this problem.
os.remove('filename')
df.to_csv('filename.csv')
Related
I am dealing with an issue on the Image.open() function from PIL in Python.
I try to open an image from a path / folder saved on a string:
path_and_filename = "c:\tmp\test.jpeg"
image = Image.open(path_and_filename)
Then I get the error:
fp = builtins.open(filename, "rb")
PermissionError: [Errno 13] Permission denied: 'c:\\tmp\\test.jpeg'
If I define the path and filename as a raw string, it works:
path_and_filename = r"c:\tmp\test.jpeg"
image = Image.open(path_and_filename)
The error message regarding "permission" makes no sense, since it works on the same file if it is definied as a raw string.
Do you see any solution or recommendetation for this issue?
Thanks a lot!
The "permission" issue was solved:
The files were copied into a hotfolder monitored by watchdog and the watchdog started the Image.open() function.
The files were sometimes not completelly copied into the folder and the function was already trying to open it. That is why I got the "permission" error message.
After setting a delay time.sleep() before calling the open function, it was possible to call it.
Note: the script was working on the raw variable since I set an already copied file name on it.
Put it in the same file path that all your .py files are and than you don’t need the path you just call the file name
Image = image.open(‘test.jpg’)
I'm trying to store a pandas dataframe to a tempfile in csv format (in windows), but am being hit by:
[Errno 13] Permission denied: 'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'
import tempfile
import pandas
with tempfile.NamedTemporaryFile() as temp:
df.to_csv(temp.name)
Where df is the dataframe. I've also tried changing the temp directory to one I am sure I have write permissions:
tempfile.tempdir='D:/Username/Temp/'
This gives me the same error message
Edit:
The tempfile appears to be locked for editing as when I change the loop to:
with tempfile.NamedTemporaryFile() as temp:
df.to_csv(temp.name + '.csv')
I can write the file in the temp directory, but then it is not automatically deleted at the end of the loop, as it is no longer a temp file.
However, if I change the code to:
with tempfile.NamedTemporaryFile(suffix='.csv') as temp:
training_data.to_csv(temp.name)
I get the same error message as before. The file is not open anywhere else.
I encountered the same error message and the issue was resolved after adding "/df.csv" to file_path.
df.to_csv('C:/Users/../df.csv', index = False)
Check your permissions and, according to this post, you can run your program as an administrator by right click and run as administrator.
We can use the to_csv command to do export a DataFrame in CSV format. Note that the code below will by default save the data into the current working directory. We can save it to a different folder by adding the foldername and a slash to the file
verticalStack.to_csv('foldername/out.csv').
Check out your working directory to make sure the CSV wrote out properly, and that you can open it! If you want, try to bring it back into python to make sure it imports properly.
newOutput = pd.read_csv('out.csv', keep_default_na=False, na_values=[""])
ref
Unlike TemporaryFile(), the user of mkstemp() is responsible for deleting the temporary file when done with it.
With the use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. mktemp() usage can be replaced easily with NamedTemporaryFile(), passing it the delete=False paramete.
Read more.
After export to CSV you can close your file with temp.close().
with tempfile.NamedTemporaryFile(delete=False) as temp:
df.to_csv(temp.name + '.csv')
temp.close()
Sometimes,you need check the file path that if you have right permission to read and write file. Especially when you use relative path.
xxx.to_csv('%s/file.csv'%(file_path), index = False)
Sometimes, it gives that error simply because there is another file with the same name and it has no permission to delete the earlier file and replace it with the new file.
So either name the file differently while saving it,
or
If you are working on Jupyter Notebook or a other similar environment, delete the file after executing the cell that reads it into memory. So that when you execute the cell which writes it to the machine, there is no other file that exists with that name.
I encountered the same error. I simply had not yet saved my entire python file. Once I saved my python file in VS code as "insertyourfilenamehere".py to documents(which is in my path), I ran my code again and I was able to save my data frame as a csv file.
As per my knowledge, this error pops up when one attempt to save the file that have been saved already and currently open in the background.
You may try closing those files first and then rerun the code.
Just give a valid path and a file name
e.g:
final_df.to_csv('D:\Study\Data Science\data sets\MNIST\sample.csv')
Edit: I deleted this but I'm going to undelete because I think it could be useful. And post what was actually happening which I didn't realize at the time.
Original Question: I'm trying to open a set of excel files where one is currently open by another user. All of the other files work, but this one I get 'permission denied' errors.
Windows gives you the option to "read only" open the file, but I can't seem to find an equivalent in python (xlrd), so I thought I would copy the file to a temp location and open it; but that doesn't work either. In either case, i get:
IOError: [Errno 13] Permission denied:
Is it possible to either:
open an excel file in read only mode (like windows) in xlrd
copy a file currently in use by another user
thanks !
As it turns out, I the files that were failing were also open by other users, so when I went to access them through windows explorer it seemed natural another user having the file open would cause the permission denied error; however the real issue is that as I was looping through the folder I was attempting to access the temp files created by Excel for the other user who was accessing the file.
So, for a given file "old_file.xlsx" that a user had open, their instance of excel created "~$old_file.xlsx"
How I got around this was:
files_to_check = [f for f in os.listdir(PATH) if os.path.isfile(f)]
files_to_check = [f for f in files_to_check if '~' not in f and 'xlsx' in f]
basically, just making sure that they're the non-temp, xlsx files that I planned to go through.
I have simple code:
from xlrd import open_workbook
open_workbook('Book1.xls')
No matter how I save the file (xls or xlsx) or how I change the path to the file (C:\Users\... or C:\Users...) I keep getting:
IOError: [Errno 2] No such file or directory: '(insert whatever is in place of Book1.xls)'
I don't know what I could be doing wrong. I'm not trying anything specific yet I just want it to not throw up an error.
You are not including whole path of the file. The path will be looks like:
file="C:\\USER\\TEST\\FILENAME"
or
file=r":\USER\TEST\FILENAME"
If you are using single slash then you need to use 'r' or else it will throw error.
you are not doing workbook("Book1.xls")
you are passing it a path (with directories) ... one of the folders in the path doesnt exist
this is probably because you do something like
directory="C:\Users\Blah\..."
when you need to do
directory = r"C:\Users\Blah\..."
other wise the backslashes serve to escape the next character
I'm getting IOError: [Errno 13] Permission denied and I don't know what is wrong wit this code.
I'm trying to read a file given an absolute path (meaning only file.asm),
and a relative path (meaning /.../file.asm), and I want the program to write the file to whatever path is given - if it is absolute, it should write it to the current dir; otherwise, to the path given.
the code:
#call to main function
if __name__ == '__main__':
assem(sys.argv[1])
import sys
def assem(myFile):
from myParser import Parser
import code
from symbolTable import SymbolTable
table=SymbolTable()
# max size of each word
WORD_SIZE = 16
# rom address to save to
rom_addrs = 0
# variable address to save to
var_addrs = 16
# new addition
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"
output = open(newFile, 'w') <==== ERROR
the error given:
IOError: [Errno 13] Permission denied: '/Use.hack'
the way I execute the code :
python assembler.py Users/***/Desktop/University/Add.asm
What am I doing wrong here?
Just Close the opened file where you are going to write.
It looks like you're trying to replace the extension with the following code:
if (myFile[-4:] == ".asm"):
newFile = myFile[:4]+".hack"
However, you appear to have the array indexes mixed up. Try the following:
if (myFile[-4:] == ".asm"):
newFile = myFile[:-4]+".hack"
Note the use of -4 instead of just 4 in the second line of code. This explains why your program is trying to create /Use.hack, which is the first four characters of your file name (/Use), with .hack appended to it.
You don't have sufficient permissions to write to the root directory. See the leading slash on the filename?
This happened to me when I was using 'shutil.copyfile' instead of 'shutil.copy'. The permissions were messed up.
I had a same problem. In my case, the user did not have write permission to the destination directory. Following command helped in my case :
chmod 777 University
Maybe You are trying to open folder with open, check it once.
For me nothing from above worked. So I solved my problem with this workaround. Just check that you have added SYSTEM in directory folder. I hope it will help somoene.
import os
# create file
#staticmethod
def create_file(path):
if not os.path.exists(path):
os.system('echo # > {}'.format(path))
# append lines to the file
split_text = text_file.split('\n')
for st in split_text:
os.system('echo {} >> {}'.format(st,path))
Check if you are implementing the code inside a could drive like box, dropbox etc. If you copy the files you are trying to implement to a local folder on your machine you should be able to get rid of the error.
For me, this was a permissions issue.
Use the 'Take Ownership' application on that specific folder.
However, this sometimes seems to work only temporarily and is not a permanent solution.
FYI I had this permission error because the file that it was trying to create was already open/used by another program (was created last time the script was run, I had opened it with excel, and then got a permission error when it was trying to recreate it)
leaving this here in case someone else finds it useful, it is not the real solution to the question asked
I got this error because the directory didn't exist yet.
Solution: create the directory
import os
if not os.path.exists(directory):
os.makedirs(directory)