"Permission denied" error when using "fopen" function in Python - python

I have the following code, and I run it from localhost:
def create_names_file(req, names, data, profileid):
s = names
fname = str(profileid)
fpath = req.conf["inf_path"]+"/"+fname
f = open(fpath, 'w')
req.conf["inf_path"] is /opt/fp/trunk/analysis/2/, and I receive permission error.I use Ubuntu OS. How can I solve this problem?

You seem to be trying to open a file named /opt/fp/trunk/analysis/2/ which in invalid due to the trailing slash. Possibly that is a typo so, if the required file already exists, who owns it?
Does the user that you run Python as have permissions to write to that file?
Check the permissions reported by ls -l /opt/fp/trunk/analysis/2.

Related

Why do I get a permission denied error while running my script in Python IDLE as administrator?

I have a web scraping script that is otherwise 100% functional. It is pointed at a website, stores attributes, paginates, and should be storing images for ConvNet processing, but I get an "PermissionError: [Errno 13] Permission denied [directory]" - when I comment out the image storage portion of the script, everything else works great.
I've tried changing from open(directory, 'wb') to urllib.request.urlopne(url) as response, open(directory,' 'wb') but of course the permission issue isn't solved.
I've run the program in CMD as admin, I've run the program directly through Python IDLE as admin, I've given permission to myself (the run as user) as well as NETWORK SERVICES and LOCAL SERVICE to the root of the storage drive, but I still receive a permission denied error.
Some unique behavior that I've observed; it creates a folder where an image should be stored, AND throws a permission denied error. The level at which the folder is created is where the image should be. The folder takes the name of the image. To eliminate variables, I tried feeding in a static string for the file name, and I still get permission denied. The folder is named "EFake.jpg", for example, when that folder should be a retrieved image in .jpg format. I've searched far and wide for a resolution to this issue, I feel like I've tried everything, apologies if I'm missing the obvious.
The entry point:
if items.get('reference_number'):
download_img_to_disk(brand, listing_url_retrieve_id, items['entry_logo'], items.get('reference_number'))
else:
download_img_to_disk(brand, listing_url_retrieve_id, items['entry_logo'])
#The function:
def download_img_to_disk(brand, name, url, reference_number='uncatogrized'):
resp = requests.get(url)
directory = "Z:\Storage\\"+str(brand)+"\\"+str(reference_number)+"\\"+name+'.jpg'
if not os.path.exists(directory):
os.makedirs(directory)
with urllib.request.urlopen(url) as response, open(directory, 'wb') as out_file:
data = response.read()
out_file.write(data)
return "File Downloaded.."
I expect "Z:\Storage\"+str(brand)+"\"+str(reference_number)+"\"+name+'.jpg' to be an actual .jpg stored, but it's a folder named name+'.jpg' and I receive the "PermissionError: [Errno 13] Permission denied [directory]" error message simultaneously. I feel like the datatype is wrong, the level at which the .jpg should be stored is wrong... I just can't figure it out.

Write a file over network in odoo with authentication

I have a Odoo8 running on my linux server and I need to copy a file from this server to a Windows 10 shared folder with authentication.
I tried to do it programmatically like this:
full_path = "smb://hostname/shared_folder/other_path"
if not os.path.exists(full_path):
os.makedirs(full_path)
full_path = os.path.join(full_path, file_name)
bin_value = stream.decode('base64')
if not os.path.exists(full_path):
try:
with open(full_path, 'wb') as fp:
fp.write(bin_value)
fp.close()
return True
except IOError:
_logger.exception("stream_save writing %s", full_path)
but even if no exception is raised, folders are not created and file is not written.
Then I tried to remove the "smb:" part from the uri and it raised an exception regarding authentication.
I'd like to fix the problem just by using python, possibly avoiding os.system calls or external scripts, but if no other way is possible, then any suggestion is welcome.
I also tried with
"//user:password#hostname"
and
"//domain;user:password#hostname"
both with and without smb
Well, I found it out by myself a way using SAMBA:
First you need to install pysmb (pip install pysmb) then:
from smb.SMBConnection import SMBConnection
conn = SMBConnection(user, password, "my_name", server, domain=domain, use_ntlm_v2 = True)
conn.connect(ip_server)
conn.createDirectory(shared_folder, sub_directory)
file_obj = open(local_path_file,'rb')
conn.storeFile(shared_folder, sub_directory+"/"+filename, file_obj)
file_obj.close()
in my case sub_directory is a whole path, thus I need to create each folder one by one (createDirectory works only this way) and everytime I need to check if the directory does not already exists because otherwise createDirectory raise an exception.
I hope my solution could be useful for others.
If anybody find a better solution, please answer...

urllib2.urlopen to open download link

i am trying to download data from a site with this code
import urllib2
source = urllib2.urlopen("http://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=WRPUPUS2&f=W").read()
open("http://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=WRPUPUS2&f=W", "wb").write(source)
and keep getting the following error,
IOError: [Errno 22] invalid mode ('w') or filename: 'http://www.eia.gov/dnav/pet/hist/LeafHandler.ashx?n=PET&s=WRPUPUS2&f=W'
I am a little confused.
Python thinks / is directory so obviously that directory does not exist, it is also not valid using /'s in a filename on unix.
You could replace the / with another character like a .: and your code will run fine
with open("www.eia.gov.dnav.pet.hist.LeafHandler.ashx?n=PET&s=WRPUPUS2&f=W", "wb") as f:
f.write(source)

Python pysmb fetching folders correct usage

I have a remote (Windows) server I'd like to connect to, and process some folder.
I tried to use SMBHandler unsuccessfully:
director = urllib2.build_opener(SMBHandler)
fh = director.open('smb://myuserID:mypassword#192.168.1.1/Publish')
It failed with the following error:
URLError: <urlopen error smb error: Failed to retrieve on Publish: Unable to open file
I wrote the following code that works perfectly using SMBConnection but I'm sure there's a better way to fetch a folder and query its subfolders and files...
conn = SMBConnection(USERID, PASSWORD, SERVER_NAME, SERVER_NAME, use_ntlm_v2 = True)
conn.connect(SERVER_IP, 139)
filelist = conn.listPath('Publish', '/')
if filelist[5].isDirectory:
print filelist[5].filename
etc...
Can you please help me finding the most elegant way to solve my problem?
Thank you very much :)
Nili
pysmb's SMBHandler does not allow you to list the files in the folder via urllib.
As of now, what you have done using SMBConnection is the recommended way to list the files in the folder.

Psycopg2 - copy_expert permission denied error

I'm attempting to make the switch from Windows to ubuntu (am using 12.04 LTS) and am trying to use some of my old scripts to run my old databases.
Previously I used postgresql and psycopg2 to maintain them and I am trying to do so again here.
My error is around importing a csv file to a table using the copy expert command.
Code is as follows:
#!/usr/bin/env python
import psycopg2 as psy
import sys
conn = psy.connect("dbname, user, host, password") # with the appropriate values
curs = conn.cursor()
table = 'tablename' # a table with the appropriate columns etc
file = 'filename' # a csv file
SQL = "COPY %s FROM '%s' WITH CSV HEADERS" % (tablename, filename)
curs.copy_expert(SQL, sys.stdin) # Error occurs here
conn.commit()
curs.close()
conn.close()
The specific error which is occurring is as follows:
psycopg2.ProgrammingError: could not open file "filename" for reading: Permission denied
Any assistance would be greatly appreciated:
I am completely stuck and I believe it is due some quirk of how I've set up the database or the files.
Adding a simple read and print command using the csv module works fine as well (from the same script in fact) It will output all of the information from the csv file and then error out with the permission denied when attempting to import it to the database
import csv
f = open(filename, 'rb')
read = csv.reader(f, delimiter =',')
for row in read:
print row
f.close()
Try executing the command as the super user by using su or sudo and if this doesn't help, the other possiblity is that the location of the filename is out of bounds so I would try copying it to the desktop or your home directory or folder where you know you definitely have full permissions and see if this works.

Categories

Resources