Change File Permission in Python File System - python

I want to change permission of file inside python file system (pyfilesystem).
Here's the code I have:
fslocal = fs.osfs.OSFS(localdir, create=True, thread_synchronize=True)
fslocal.setcontents('/file1', b'This is file 1')
Now I want to change file permission of file 1. I am using os.chmod for this
os.chmod(localdir + '/file1', stat.S_IWOTH)
However, I get this error:
Traceback (most recent call last):
File "/Users/user/.conda/envs/scram/lib/python3.4/site-packages/fs/errors.py", line 257, in wrapper
return func(self,*args,**kwds)
File "/Users/user/.conda/envs/scram/lib/python3.4/site-packages/fs/osfs/__init__.py", line 251, in listdir
listing = os.listdir(sys_path)
PermissionError: [Errno 13] Permission denied: '/Users/user/Documents/tests/function/arwan/localfs/file1
Can you please tell me if it is possible to do it and how?
Thanks.

One problem is that it appears you're calling chmod with the intent of adding a single permission bit. In fact, you are setting all of the permission bits, so the call is trying to clear all of them except the one you want set. Assuming you're on a Unix system, you will presumably want to set the user and group bits as well, including the read and execute bits.
You can do the following:
st = os.stat(path)
old_mode = st.st_mode
new_mode = old_mode | stat.S_IWOTH
os.chmod(path, new_mode)
Hopefully that will help you.

Related

Problem downloading MEGA files with python

I am trying to download a file from my MEGA account using the following code from the mega.py library of Python:
from mega import Mega
mega = Mega()
m = mega.login('example#example.com', 'example')
file = m.find('example.txt')
m.download(file, 'D:\\Desktop')
However, it is always returning:
Traceback (most recent call last):
File "D:\Programas\aNaconda\lib\shutil.py", line 788, in move
os.rename(src, real_dst)
PermissionError: [WinError 32] The file is already being used by another process: 'C:\\Users\\vrida\\AppData\\Local\\Temp\\megapy_xdste432' -> 'example.txt'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<ipython-input-26-c3f75106fafb>", line 1, in <module>
m.download(file)
File "D:\Programas\aNaconda\lib\site-packages\mega\mega.py", line 564, in download
return self._download_file(file_handle=None,
File "D:\Programas\aNaconda\lib\site-packages\mega\mega.py", line 745, in _download_file
shutil.move(temp_output_file.name, output_path)
File "D:\Programas\aNaconda\lib\shutil.py", line 803, in move
os.unlink(src)
PermissionError: [WinError 32] The file is already being used by another process: 'C:\\Users\\vrida\\AppData\\Local\\Temp\\megapy_example'
Actually when I enter in the folder (C:\Users\vrida\AppData\Local\Temp) I find a temporary file like the one I'm wanting to download but named megapy_example.
I saw that the following site has a discussion to solve the problem:
https://www.reddit.com/r/learnpython/comments/mw6is2/download_file_from_mega_using_megapy/
asking to add the following lines to the code:
try:
m.download(file, 'D:\\Desktop')
except PermissionError:
continue
In my case, the continue command wasn't working so I simply put in the pass command. The code runs, but I don't know if the file is really saved or not.
Could someone please help me? I really need to download the files and save them.
If it doesn't work through the mega.py library, you guys would somehow know how to download from the public link like this by Python:
https://mega.co.nz/#!cSZCELDb!5O57KMVMIgrPiH5fnaefWeNPDqoDWzGbY-sZkdTUdNk
There's a bug in the library, it's not closing the file before moving it. You could fix the bug by editing the source code:
Open the file at D:\Programas\aNaconda\lib\site-packages\mega\mega.py
Goto line 745 where the line shutil.move(temp_output_file.name, output_path) is.
Add temp_output_file.close() right above it.
Save & try again.

PermissionError: [Errno 13] Permission denied when saving HoloMap to GIF

I am trying to create an animated gig from a series of heat maps with HoloViews.
I need to do this in a Python script, i. e. specifically not in a Jupyter notebook.
When saving the image, Python throws an error because it cannot create a temporary file in the temp-folder of the current user (this is under Windows). Happens regardless of the user, even when I run Python as admin.
When I stop in the debugger and change the temp-file path to some other place, e. g. Desktop, that works, but the resulting holo.gif in the working directory is empty (0 bytes). The temporary gif, though, is correctly animated, so I guess the code is basically OK.
[Edit: Not so sure anymore. I ran this the night through on 26.531 heat maps each of which consisted of a 5x5 grid. The process did not finish (i. e. did not hit the breakppoint at Image.py line 1966). Is there a way to do what I want that is less painfully slow?]
Answers to similar problems on StackOverflow did point to permission problems (but what kind of problem could that be if it doesn't even work for an admin?) and suggest saving to another location, which is impossible here as I have no control over where matplotlib will try to create temporary files.
The problem is specifically with gif's, I can create *.png or *.html output without error. (AFAIK, the difference is that gif-creation uses ImageMagick.)
Here's the code (construction of underlying heat map data left out):
import holoviews as hv
hv.extension('matplotlib')
renderer = hv.renderer('matplotlib')
renderer.fps = 3
heatMapDict = {
k: hv.HeatMap(measurements[k].sensors) for k in range(len(measurements))
}
holo = hv.HoloMap(heatMapDict, kdims='index')
renderer.save(holo, 'holo', fmt='gif')
And the traceback:
INFO:matplotlib.animation:Animation.save using <class 'matplotlib.animation.PillowWriter'>
Traceback (most recent call last):
File "cm3.py", line 69, in <module>
renderer.save(holo, 'holo', fmt='gif')
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\holoviews\plotting\renderer.py", line 554, in save
rendered = self_or_cls(plot, fmt)
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\holoviews\plotting\mpl\renderer.py", line 108, in __call__
data = self._figure_data(plot, fmt, **({'dpi':self.dpi} if self.dpi else {}))
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\holoviews\plotting\mpl\renderer.py", line 196, in _figure_data
data = self._anim_data(anim, fmt)
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\holoviews\plotting\mpl\renderer.py", line 246, in _anim_data
anim.save(f.name, writer=writer, **anim_kwargs)
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 1174, in save
writer.grab_frame(**savefig_kwargs)
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\contextlib.py", line 119, in __exit__
next(self.gen)
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 232, in saving
self.finish()
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\matplotlib\animation.py", line 583, in finish
duration=int(1000 / self.fps))
File "C:\Users\y2046\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\Image.py", line 1966, in save
fp = builtins.open(filename, "w+b")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\y2046\\AppData\\Local\\Temp\\tmp4im5ozo8.gif'
Addendum:
I'm coming to think that this is not a permission problem after all. Perhaps it has to do with reentrancy and file-locking under Windows? The Python process in fact may create files in the temp directory, as proved by inserting the following test code before calling renderer.save():
import os
import builtins
filename = 'C:\\Users\\y2046\\AppData\\Local\\Temp\\test.txt'
fp = builtins.open(filename, "w+b")
try:
fp.write("first".encode('utf-8'))
finally:
fp.close()
os.remove(filename)
I should test this under Linux. If it works there, there must be a bug in the Pillow writer.
It looks like there is something broken with HoloViews. I have opened issue #3151 with them.

root directory IOError: [Errno 13] Permission denied:

I am working on Ubuntu and writing a code in python. I want to add a line in a file which is placed in root directory:
ins = open( "/usr/local/etc/conf.d/test.txt", "r" )
array = []
for line in ins:
array.append( line )
array.append('add this new line')
f = open("/usr/local/etc/gnuradio/test.txt",'w')
for line in array:
f.write(line)
I am getting this error:
Traceback (most recent call last):
File "overwrite.py", line 6, in <module>
f = open("/usr/local/etc/gnuradio/test.txt",'w')
IOError: [Errno 13] Permission denied: '/usr/local/etc/gnuradio/test.txt'
I know we do not have permission to change anything in root directory without using sudo. But is there anyway I can update this file from within my python module?
You already answered your own question: You do not have the permission to do so.
No matter if you use sh, bash, python, C, erlang or a rubber-hose attack.
Either run your script with a user owning the necessary permissions or grant yourself access to the file.

Python error "IOError: [Errno 2] No such file or directory" but file is there

I am trying to read a csv file and I am getting the error above but the file is there. The line giving the error is
infilequery = file('D:\x88_2.csv','rb')
and I get the error below.
Traceback (most recent call last):
File "C:\Python26\usrapply_onemol2.py", line 14, in
infilequery = file('D:\x88_2.csv','rb')
IOError: [Errno 2] No such file or directory: 'D:\x88_2.csv'
I can put a file from the same directory in its place and python at least sees it. The result of os.listdir("D:") features 'x88_2.csv' and the result of "dir D:\" also includes it. When putting the filename in and allowing python to complete the path and selecting x88_2.csv from the dropdown, I still get the same error. What is up here?
Try
'D:\\x88_2.csv'
The \x88 is interpreted as the character at code point 0x88. Alternatively you could use raw string
r'D:\x88_2.csv'
or forward slash
'D:/x88_2.csv'

Mercurial: Permission Denied for hgwebdir

Yesterday I setup Apache to serve my Mercurial repositories and got everything working properly. I then tested pushing changes back to this repository and was presented with an error, and now that error pops up for every single operation I attempt - even just a simple GET request of the repositories! Here is the error:
mod_wsgi (pid=1771): Target WSGI script '/var/hg/hgweb.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=1771): Exception occurred processing WSGI script '/var/hg/hgweb.wsgi'.
Traceback (most recent call last):
File "/var/hg/hgweb.wsgi", line 18, in ?
application = hgwebdir(config)
File "/usr/lib64/python2.4/site-packages/mercurial/hgweb/__init__.py", line 15, in hgwebdir
return hgwebdir_mod.hgwebdir(*args, **kwargs)
File "/usr/lib64/python2.4/site-packages/mercurial/hgweb/hgwebdir_mod.py", line 52, in __init__
self.refresh()
File "/usr/lib64/python2.4/site-packages/mercurial/hgweb/hgwebdir_mod.py", line 82, in refresh
self.repos = findrepos(paths)
File "/usr/lib64/python2.4/site-packages/mercurial/hgweb/hgwebdir_mod.py", line 36, in findrepos
for path in util.walkrepos(roothead, followsym=True, recurse=recurse):
File "/usr/lib64/python2.4/site-packages/mercurial/util.py", line 1164, in walkrepos
for hgname in walkrepos(fname, True, seen_dirs):
File "/usr/lib64/python2.4/site-packages/mercurial/util.py", line 1146, in walkrepos
for root, dirs, files in os.walk(path, topdown=True, onerror=errhandler):
File "/usr/lib64/python2.4/os.py", line 276, in walk
onerror(err)
File "/usr/lib64/python2.4/site-packages/mercurial/util.py", line 1127, in errhandler
raise err
OSError: [Errno 13] Permission denied: './dev/fd'
My repository directory is owned by apache, the user running Apache. I dont know why './dev/fd' is being operated on either. I've restarted the server numerous times, recreated the repository directory, but I still get this error no matter what! I dont have access to restart the machine, so that is not an option. But it seems to have gotten in a very bad persistent state, and I dont know how to fix it. Any help is appreciated!
This turned out to be a configuration error on my part, and rather than delete the question I'll post the resolution here in case someone has this problem in the future.
Here was the hgweb.config I was using:
[paths]
/ = /var/hg/repos/*
#[web]
style = gitweb
allow_archive = bz2 gz zip
maxchanges = 200
allow_push = *
push_ssl = false
Two problems here, one is obvious. I had the [web] header commented out, and I assume that many of the options are not valid for the [paths] section. Also, after re-reading the Hg docs again, the push_ssl directive does not belong in the hgweb.config file, but rather in each repository's .hg/hgrc (or the ~/.hgrc of the user that runs apache). After fixing these, things are working perfectly!

Categories