File permissions with python - python

I have an xml file on a windows network drive that I am trying to read and write to. I have full permission to edit the file normally (in gedit or anything), but when I attempt to parse the xml file in python, I get a permissions error. Any ideas?
Here is an example of what I am trying to do:
from xml.etree import ElementTree as ET
file = "/directory/to/xml/file"
nfo = ET.parse(file)
I then read info from the file and write to the file. It hangs up when trying to write to the file (even though when I use gedit, I can write to the file fine)
the error I get is:
Traceback (most recent call last):
File "parser.py", line 277, in <module>
nfo.write(file)
File "/usr/lib/python2.7/xml/etree/ElementTree.py", line 803, in write
file = open(file_or_filename, "wb")
IOError: [Errno 13] Permission denied: '/directory/to/xml/file'

are you sure that gedit does not ignore lack of write permission if you're the file owner? vim can do that, and maybe gedit too? I'd start with simple ls -l /directory/to/xml/file.
you can always do strace -f /path/to/your_program.py, that should give you more info on what is actually being attempted by python runtime.

Related

Having an issue running Python Code in terminal

Hi everyone so I have some python code I am trying to run from my terminal (will not work in an IDE) to take a .mp3 file and classify the genre of the song based on the Spectrogram that we are using the librosa library plugin for. The code is from this gitHub: https://github.com/cetinsamet/music-genre-classification .When I use the command prompt specified by the gitHub user who created this app I get this error in my terminal:
(base) Nicos-MacBook-Pro:src nico$ python3 get_genre.py ../test.mp3
Traceback (most recent call last):
File "get_genre.py", line 61, in <module>
main(sys.argv[1:])
File "get_genre.py", line 30, in main
net.load_state_dict(torch.load(MODELPATH, map_location='cpu'))
File "/Users/nico/opt/anaconda3/lib/python3.7/site-packages/torch/serialization.py", line 419, in load
f = open(f, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '../utils/net.pt'
Here is the command line specified: $ python3 get_genre.py ../test.mp3
The error message quite plainly tells you that the code depends on having a file ../utils/net.pt

Error 13 Permission denied when trying to write on unrelated folder

I dont have much experience in python, so it might be a stupid question.
Im trying to write to a file in my script, if I run the script from his folder, it works, however if I run it from another folder, I have an error 13 persmission denied
i.e. in cmd :
cd C:\Users\user010\Perforce\Build\LS3\
py Build_jenkins.py
works but
cd C:\Program Files (x86)\Jenkins\workspace\LC3.3 Test\Qt\main
py C:\Users\user010\Perforce\Build\LS3\Build_jenkins.py
doesnt work. I have tried moving the script and the file to a public folder, same error :
Traceback (most recent call last):
File "C:\Users\Public\Documents\Build\LS3\Build_jenkins.py", line 159, in <module>
Variables.Sauvegarder()
File "C:/Users/Public/Documents/Build/LS3/../Common\Variables.py", line 87, in Sauvegarder
gArbre.write( NOM_FICHIER )
File "C:\Python34\lib\xml\etree\ElementTree.py", line 761, in write
with _get_writer(file_or_filename, encoding) as write:
File "C:\Python34\lib\contextlib.py", line 59, in __enter__
return next(self.gen)
File "C:\Python34\lib\xml\etree\ElementTree.py", line 798, in _get_writer
errors="xmlcharrefreplace")
PermissionError: [Errno 13] Permission denied: 'Variables.xml'
The code is Variables.Sauvegarder() in my main file and in Variables :
import xml.etree.ElementTree as ET
gArbre = ET.parse( "Variables.xml" )
def Sauvegarder():
gArbre.write( "Variables.xml")
Edit : important detail I forgot to mention, the file im writing to is located in the script folder (C:/Users/Public/Documents/Build/LS3/../Common\Variables.xml)
To be honest, I'm not sure about Windows computers (for a mac or other linux-based system you'd use sudo), but I would recommend trying to run the command as an administrator (use the runas command plus the name of an account with admin priveledges.) That should hopefully fix the error you're getting.

Opening a file in Python gives me errors

I am quite new to Python and I am having problems opening a file in Python.
I want to open a text file called 'coolStuff' in a Folder on my Desktop and this is how I type in the command but I still get an error message. The file exists and so I do not understand why I get that error message.
open("coolStuff.txt","r")
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
open("coolStuff.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'coolStuff.txt'
If you want to simply supply a filename like coolStuff.txt without also providing a full directory, then you have to make sure that Python is running in the same directory as the file. If you aren't sure what directory Python is running in, try this:
import os
print(os.getcwd())
You have two options:
let's say your file is in C:\path\to\dir\coolStuff.txt
1.
open(r'C:\path\to\dir\coolStuff.txt','r')
2.
import os
os.chdir(r'c:\path\to\dir')
open('coolStuff.txt', 'r')
Because the file you want to open is not in the current directory.You can find the file 'coolStuff.txt' in the terminal and launch your python environment at the same directory.

Python - "IOError: [Errno 13] Permission denied" when running cron job but not when running from command line

I have SSHed from my local machine (a Mac) to a remote machine called “ten-thousand-dollar-bill” as the user “chilge”.
I want to run a Python script in the folder “/afs/athena.mit.edu/c/h/chilge/web_scripts” that generates and saves a .png image to the folder “/afs/athena.mit.edu/c/h/chilge/www/TAF_figures/KORD/1407”. When I run the script from the command line, the image is generated and saved without any issues. When I run the script as cron job, though (the crontab resides in “/afs/athena.mit.edu/c/h/chilge/cron_scripts”), I get the following error:
Traceback (most recent call last):
File "/afs/athena.mit.edu/user/c/h/chilge/web_scripts/generate_plots.py", line 15, in
save_taffig(taf,fig)
File "/afs/athena.mit.edu/user/c/h/chilge/web_scripts/plotting.py", line 928, in save_taffig
fig.savefig(os.getcwd()+'/'+savename+'.png')
File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1084, in savefig
self.canvas.print_figure(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 1923, in print_figure
**kwargs)
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 443, in print_png
filename_or_obj = file(filename_or_obj, 'wb')
IOError: [Errno 13] Permission denied: '/afs/athena.mit.edu/user/c/h/chilge/www/TAF_figures/KORD/1407/140723-1200_AMD_140723-1558.png'
I believe I’ve correctly changed the permissions of all of the necessary directories, but I’m still getting this error. I am not sure why the script would run fine from the command line, but fail when I try to run the script as a cron job.
(Also, I’m not sure if this will be relevant, but don’t have sudo permissions on the remote machine.)
Maybe an other software opens the file which you want to overwrite?

Error opening megawarc archive from Python

I've found myself having to use a python script to access a webarchive.
What I have is a 'megawarc' web archive file from http://archive.org/details/archiveteam-fanfiction-warc-11. I need to un-megawarc this, using the python script found at https://github.com/alard/megawarc.
I'm trying to run the 'restore' command, and I have the three files needed (FILE.warc.gz,
FILE.tar, and FILE.json.gz) from the first link.
I have both python 2.7 and 3.3 installed.
--------------update--------------
I've ran both this method..
python megawarc restore FILE
and this method..
Make sure you have the files megawarc and ordereddict.py in the same directory, with the files you want to convert.
Rename the file megawarc to megawarc.py
Open a python console in this directory
Type the following code (line by line) :
import sys
sys.argv = ['megawarc','restore','FILE']
import megawarc
megawarc.main()
using python 2.7, and this is what I get..
c:\Python27>python megawarc restore FILE
Traceback (most recent call last):
File "megawarc", line 563, in <module>
main()
File "megawarc", line 552, in main
mwr.process()
File "megawarc", line 460, in process
self.process_entry(entry, tar_out)
File "megawarc", line 478, in process_entry
entry["target"]["offset"], entry["target"]["size"])
File "megawarc", line 128, in copy_to_stream
raise Exception("End of file: %d bytes expected, but %d bytes read." % (buf_size, l))
Exception: End of file: 4096 bytes expected, but 236 bytes read.
Is there something else i'm missing?
I have the following files all in
c:\python27
FILE.megawarc.json.gz
FILE.megawarc.tar
FILE.megawarc.warc.gz
megawarc
ordereddict.py
Is this some type of corrupt file error? Is there something i'm missing?
On the second link you provided, there are two important files :
megawarc
ordereddict.py
The executable script is megawarc. To run it, you have to launch it in a shell with
python megawarc restore FILE
Alternatively, if you're using a UNIX-based system. You can do
chmod +x megawarc
To give megawarc script executable property and then run it with
./megawarc restore FILE
Here, FILE is the actual name you should type if the 3 files you have are FILE.warc.gz, FILE.tar, and FILE.json.gz. You have to change this parameter by the common prefix to your 3 input files if needed.
EDIT :
Okay, i found an alternative that would work if you don't have a standard shell to start the script in command line.
What you have to do is :
Make sure you have the files megawarc and ordereddict.py in the same directory, with the files you want to convert.
Rename the file megawarc to megawarc.py
Open a python console in this directory
Type the following code (line by line) :
import sys
sys.argv = ['megawarc','restore','FILE']
import megawarc
megawarc.main()
This should work, i've just tried it.
Hope it will help.

Categories