I am placing this code into IDLE:
f = open('/Users/alex/Documents/URM8/health.tdf')
I don't understand why I am unable to open it. I get the error:
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
f = open('/Users/alex/Documents/URM8/health.tdf')
IOError: [Errno 2] No such file or directory: '/Users/alex/Documents/URM8/health.tdf'
Of course usually the problem is filename. I have checked it lots of times and it is correct.
I 'unlocked' the file (I'm using Mac OSX). Also set write access to Everyone in Mac OSX.
Do i need to set permissions in Bash?
Really appreciate someone telling me what I'm doing wrong!
/Users/alex/Documents/URM8/health.tdf cannot be opened because it's not there; the Mac OS UI hides the .txt extension. open('/Users/alex/Documents/URM8/health.tdf.txt') works fine.
The problem is not the permissions. If it were, the error message would be different. Is some component of the path a Mac alias to a directory, rather than a directory? If so, Python won't follow it, and will give that error.
Try individual parts of the pathname to see exactly which directory or file Python can't find. You could do this simply using cd in the shell.
Are you trying to use a relative path? The leading '/' could be a problem.
You don't specifically define the file mode ("r", "w", etc.) in your open call, you may want to reconsider this.
You could try ls -l on the file to get its permissions.
chmod u+rw <FILE> should give you access.
Related
def hmsbookings() :
os.system('python hmsbookings.py')
root.after(60000, hmsbookings)
I tried these both lines, but for some reasons, it's not working out.
It shows error :
/System/.../MacOS/Python: can't open file 'hmsbookings.py': [Errno 2] No such file or directory.
The execution path could be different where the file is located.
In your case, the execution is in /System/.../MacOS/Python however your file is somewhere else. This misbehavior could be resolved if you use the full path of the file.
Let me assume, that your file is located in your Desktop. Then this is the modified code that uses absolute path.
import os
def hmsbookings() :
os.system('python os.path.expanduser("~/Desktop/hmsbookings.py")')
root.after(60000, hmsbookings)
I'm not familiar with macOS, I based on this question. If you need more information about absolute path on mac see this question.
I made a python script which should start when I start my PC in order to do that I created a simple .bat file like this. But now I have a problem because why ever windows isn't able to open the database connection on startup. I tried it like this:
conn = sqlite3.connect('DB\Todos.db')
When I execute the baatch file normaly everything works but when windows starts it on startup this error comes up:
Traceback (most recent call last):
File "Path to my file", line 41, in <module>
conn = sqlite3.connect('DB\Todos.db')
sqlite3.OperationalError: unable to open database file
Of course where "Path to my file" is the normal path.
Thanks for your help
Of course this show that the system cannot find the specified file, this are the reason to they can't open the database file. If your program is in DB folder, you can just use
conn = sqlite3.connect('Todos.db') instead. And, the most recommended if is for personal purposes, is that you use the entire path, like conn = sqlite3.connect('X:\MyDB\SQLite\Todos.db'). The output error sqlite3.OperationalError: unable to open database file begins, normally, with these errors below:
System cannot find the file.
System cannot open the file.
System doesn't have the permission to the file (or you don't have).
System doesn't have READ permissions.
If cannot open find the file, move your script or your directory to the place that the file is. It's economize code, is much better than insert the full path, because if you have making the program to someone, is harder to say where the program is, and he/she will, probably, change the code, etc. But if everything is on a same folder, is a lot easy.
If cannot open the file, probably is corrupted; the program cannot read this ext of file or is not a program that he recognize. Or you don't pay the program.
If don't have permission to the file, you need to start the program as ADMINISTRATOR, if it doesn't work, search in the internet how to give permissions to an .(yourext).
For least, READ permissions is right on the file. Some files has an attrib, the "-r", the Read-only file attribute, remove it with an PROMPT (CMD) with admin rights, being in the folder of the file and typing attrib -r -s -h yourfile.ext, it removes the read-only, system and hidden file attributes.
If everything doesn't work, the file is corrupted. Sorry.
This code
from PIL import ImageGrab, Image
im = ImageGrab.grab()
im.save(r'D:/MyFilesForProject/Network/screen.jpg')
cause this error
Traceback (most recent call last):
File "G:/PycharmProjects/2.7NetworkExersice/Screenshot.py", line 4, in
<module>
im.save(r'D:\MyFilesForProject\Network\screen.jpg')
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1676, in save
fp = builtins.open(fp, "wb")
IOError: [Errno 13] Permission denied:
'D:\\MyFilesForProject\\Network\\screen.jpg'
I tried to find a solution in the whole internet and I did not find anything working. In order to avoid repetitive advice: yes, I allowed reading and writing the folder in the properties and tried to save it in another place. I really hope for your help. Thank you in advance.
THere is a problem with Python having Permission to write to that drive.
Try to run Python as an Admin:
From commandline:
runas.exe /user:administrator "C:\Python27\python.exe Screenshot.py"
I managed to replicate your error by attempting to save to folder that did not have write permissions.
This is how I fixed the error:
Right click on the folder you wish to save to.
Click properties at the bottom of the list
Go to the security tab
Click the Edit button next to To change permissions, click Edit
make sure the write section has a tick in the Allow column for the user you are logged in as. If this is not a private folder or a security risk you can allow for everyone
One other thing I noted was I had the error from the updated fix
File "C:\Anaconda3\lib\site-packages\PIL\Image.py", line 1725, in save
fp = builtins.open(filename, "w+b")
note the w+b whereas in your traceback you have wb
This could be due to using an outdated version of PIL..
Try uninstalling PIL via PIP and then reinstalling the latest version with
pip install pil
One other difference is that I ran this on Python 3.6 not 2.7 like yourself.
I am using netCDF4 and python 3.4.
I run the following line of code in order to start writing a new netCDF file that I will be filling with data later in my code following netcdf4 documentation. I however keep getting this error...
File "netCDF4.pyx", line 1466, in netCDF4.Dataset.__init__ (netCDF4.c:19692) RuntimeError: Permission denied
from netCDF4 import Dataset
rootgrp = Dataset('test.nc', 'w', format='NETCDF4')
Any help will be most appreciated.
The "Permission denied" part of the error leads me to believe you don't have permissions to write to the current directory (wherever you are when you run your script).
Check your permissions and/or try giving a full path to put the file in a directory you know you can write to.
dataset.close()
or close ide and delete file 'netCDF4.pyx'
maybe a file exist there, so can't write it.
In my case, this error arises when I am running python by crontab.
The solution is to add following line at the beginning of crontab file.
HDF5_USE_FILE_LOCKING=FALSE
When I try to create a new file in PyScriptor using the following code, it works perfectly with no errors but when I use the same code outside of PyScriptor for example in the IDE, it just flashes an error and closes.
file = open("file.txt", "w")
file.write("hello")
file.close()
I cannot get the errors because the error message lasts for less than a second before the Interpreter closes.
Help to figure out why this happens and how to fix it is appreciated.
I am using Python version 2.5.3.0 on windows 7 if this helps.
[EDIT]
Thanks to kirbyfan64sos and JAB for helping me get the errors. It turns out that Python does not have permission to open any file at all (r, w, a etc) yet Pyscriptor has all these permissions so the question still remains: why are they be different and how do i fix them?
I know that it is not the permissions of the file itself otherwise it would not work in Pyscriptor.
[ERROR CODE]
Traceback (most recent call last):
File "IDE File test.py", line 1, in <module>
file = open("file.txt", "w")
IOError: [Errno 13] Permission denied: "file.txt"
Use "w" instead of "W". "W" is an invalid file mode. Also, file.close should be file.close(), as file.close will just return the file object's close method and won't actually call it.
Note that if you start the interpreter from inside the command shell, the shell won't close when an error is raised so you'll be able to see it there. The same applies to using IDLE.
Chances are the permissions of wherever your IDE is running in are not writable by you.
To check:
import os
print(os.path.abspath(os.curdir))
I'm quite certain that you'll find the current path will be different depending on which IDE you run from. Most IDEs have a setting for which path you'll start at. You can also get around that by using os.chdir to change to your home directory (or giving an absolute path to open(), e.g. open('/home/wayne/test.txt', 'w'))