I'm trying to run this in the Python REPL:
file = open('/dev/sda1','rb')
However, I am getting this error:
Traceback (most recent call last):
File <stdin>, line 1, in <module>
IOError: [Errno 13] Permission denied: /dev/sda1
How do I gain access to /dev/sda1?
You likely have to be root to read /dev/sda1. You usually can run sudo to run things as root. For example, to run myscript.py:
sudo python myscript.py
Or to run the REPL:
sudo python
You must be a member of the appropriate group to use sudo. Additionally, your system may not have sudo. If your system does not have sudo, you may have to use su. If your system does not have su, you may have to log in as root.
For more information on sudo, type man sudo.
For more information on su, type man su.
Related
I am using Python3 on Ubuntu with the HID module python3-hid.
If I use it to open a device, I simply get back:
OSError: open failed
Which prompted me to handle the exception, and see if I can get more information out of it:
try:
h.open(vend, prod)
except OSError as error :
print(error)
print(error.strerror)
print(error.filename)
sys.exit(1)
To my surprise, no further information was available, as both strerror and filename were None.
open failed
None
None
NOTE: My open() succeeds when I run as root, so I know this is a permission thing. But why can't this OSError tell me that it is permission related?
How do I get all the details (specific reason of failure, and filename involved) out of an OSError?
UPDATE: The traceback leads to hid.pyx file:
Traceback (most recent call last):
File "/tmp/./tst.py", line 30, in <module>
h.open(vend, prod)
File "hid.pyx", line 66, in hid.device.open
I am not sure why my system can't find the hid.pyx file.
$ dpkg -S hid.pyx
dpkg-query: no path found matching pattern *hid.pyx*
Checking the upstream package from Ubuntu... it turns out that python3-hid is part of the software for a crypto coin wallet? Strange. I think I will skip using this module, and maybe write directly in C, using libhidapi-hidraw which this python module also uses.
The code is absolutely correct, here is the link to the repository from where I cloned it: https://github.com/nft-fun/generate-bitbirds.
I did not use the in-built vs code one-it was having some other issues, used other but already activated using conda init beforehand
The only prerequisite was to install the dependencies and so I did.
After doing that I activated the base conda env then run the py script, and this is what it shows.
C:\Users\Zee\Documents\NFT_Tests\generate-bitbirds (main -> origin)
(base) λ python bitbird_generation_script.py
Traceback (most recent call last):
File "bitbird_generation_script.py", line 244, in <module>
new_image.save(imgname)
File "C:\Users\Zee\anaconda3\lib\site-packages\PIL\Image.py", line 2232, in save
fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: '/bird_images/0.png'
Any insights would be helpful :)
You have to find out what is the issue.
Inside the script, check the value of dirname = os.path.dirname(__file__).
The error you encounter seems to be that dirname is the empty string and so the images are trying to get saved in /bird_images which does not exist.
It should try to save the images in the local directory ./bird_images/
You could try :
On Linux :
PYTHONPATH=. python bitbird_generation_script.py
On Windows:
set PYTHONPATH=.
python bitbird_generation_script.py
I get an "operation not permitted" traceback with trying to use os.setegid(). I've found a few answers to similar questions to this one, but the questions are more complicated than my case, and the answers presume the users did a setuid first, or suggest setting the setgid bit on the directory.
This issue is much simpler. I'm just doing a os.setegid with group id that I am a member of. In the shell I can use sg(), newgrp(), or chgrp() with the same group successfully. Why does this python command fail?
>>> import os
>>> os.setegid(34007)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 1] Operation not permitted
>>>
I am using RHEL7 and an older version of python (2.7.5) but the same error occurs with python 3.6.10, though in that case it's a PermissionError rather than an OSError.
Ensure you're running as root.
https://www.geeksforgeeks.org/python-os-getegid-and-os-setegid-method/
...method is typically available only to the superuser. Superuser
means a root user or an administrative user who has all the
permissions to run or execute any program in the operating system
I am trying to run two python scripts on startup on my Raspberry Pi.
Both scripts work if I launch them with
python script1.py
python script2.py
They also work with a shell script in the same folder containing
python script1.py &
python script2.py &
But if I add either the individual scripts to the rc.local startup file, or the shell script, I get errors of missing files.
If I add the python scripts to startup via rc.local then it claims that text files I call in the script are missing:
pi#raspberrypi ~ $ sudo service rc.local start
My IP address is 192.168.0.4
pi#raspberrypi ~ $ Traceback (most recent call last):
File "home/pi/scripts/script1.py", line 8, in <module>
scripts.make_batch(randint(10,12))
File "/home/pi/scripts/functions.py", line 36, in make_batch
num_lines = sum(1 for line in open('script1_file.txt'))
IOError: [Errno 2] No such file or directory: 'script1_file.txt'
Traceback (most recent call last):
File "home/pi/scripts/script2.py", line 5, in <module>
scripts.check(3,30)
File "/home/pi/scripts/functions.py", line 78, in check
with open('script2_file.txt', 'r+') as followed:
IOError: [Errno 2] No such file or directory: 'script2_file.txt'
^C
If I add the batch script.sh to startup that calls the python scripts then it claims they are missing.
pi#raspberrypi /etc $ sudo service rc.local start
My IP address is 192.168.0.4
pi#raspberrypi /etc $ python: can't open file 'script1.py': [Errno 2] No such file or directory
python: can't open file 'script2.py': [Errno 2] No such file or directory
^C
Is this something to do with permissions? My script folder is 755 recursive.
All the files ARE there and the scripts run fine directly. This is something to do with running files within files through the rc.local startup process... but I don't know what!
Help greatly appreciated.
You need to supply the full paths to the files:
with open('full/path/to/script2_file.txt', 'r+')
The same for your second problem:
'full/path/to/script2.py'
I'm have a problem where if I double click my script (.py), or open it with IDLE, it will compile and run correct. However, if I try to run the script in my windows command line, using
C:\> "C:\Software_Dev\Python 2.7.1\python.exe" C:\path\to\script\script.py
I get...
Traceback (most recent call last):
File "C:\path\to\script\script.py", line 66, in <module>
a.CheckTorrent()
File "C:\path\to\script\script.py", line 33, in script
self.WriteLog(fileName)
File "C:\path\to\script\script.py", line 54, in WriteLog
myFile = open(r'%s' %(filename), 'w')
IOError: [Errno 13] Permission denied: './TorrentMonitor.log'
So my question is, why am I getting permission errors when I run this script through command line in window 7 but not when I double click? What's the difference between those two processes?
Thanks in advance!
The script is trying to write into a file in the current directory. In the example above, you're starting it from C:\ where you probably don't have write permissions.
cd to a directory that you own, and you should be able to run that command just fine.
This is because when you double-click the file (or when running it from IDLE), the current working directory is the directory that contains your script. When starting it from the command line, it's C:\ which you don't seem to have write access to.