Error with creating a csv file in python: Operation not permitted - python-2.7

I am trying to create an empty csv file in python (2.7) but see the following error. Can anyone tell me why this is happening?
>>> tutorial_out = open('tutorial.csv', 'wb')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 1] Operation not permitted: 'tutorial.csv'

This is a permission issue, you probably don't have permission to write in that directory.
Solution: Modify permission of that directory or run as root.

Related

Python script run as root Permission denied

I am configuring tmda on debian 8. I am running tmda-filter.py as root.
This script reads a key file. that it insists be CHMOD 400 or 600. The script executes from /package/tmda-fork/tmda/bin/tmda-filter belonging to
ROOT:ROOT
The key is in /vpopmail/domains/.tmda/crypt_key owned by ROOT:ROOT
Traceback (most recent call last):
File "/package/tmda-fork/tmda/bin/tmda-filter", line 50, in <module>
execfile(os.path.join(execdir, 'tmda-rfilter'))
File "/package/tmda-fork/tmda/bin/tmda-rfilter", line 164, in <module>
from TMDA import Defaults
File "/package/tmda-fork/tmda/TMDA/Defaults.py", line 1753, in <module>
CRYPT_KEY = binascii.unhexlify(open(CRYPT_KEY_FILE).read().strip())
IOError: [Errno 13] Permission denied: '/home/vpopmail/domains/mydomain.com/.tmda/crypt_key'
Change owners on key file to directory owners and it executed.
chown vpopmail:vchkpw

create file containing '/' in file name in python

how can I create a file in python if the filename contains '/'
url='https://www.udacity.com/cs101x/index.html'
f=open(url,'w')
f.write('123')
f.close()
above code produces an error as
Traceback (most recent call last):
File "9.py", line 2, in <module>
f=open(url,'w')
IOError: [Errno 22] invalid mode ('w') or filename:https://www.udacity.com/cs101x/index.html'
Use os.path.basename() to isolate the filename.
import os
url='https://www.udacity.com/cs101x/index.html'
filename = os.path.basename(url)
f=open(filename,'w')
f.write('123')
f.close()
This will create a file called index.html

Http 404 when trying to get files with special characters from GitLab API

I am using python-gitlab driver for the Gitlab API. The following work fine on my repository:
>>> rf_blob = p.repository_blob(p_latest_commit.id, 'iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMOAuth2Authentication.h')
>>> rf_blob = p.repository_blob(p_latest_commit.id, 'iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMOAuth2SignIn.h')
However, when I try to open a file that contains a + character in it, I get the following even though that file clearly exists in the repository:
>>> rf_blob = p.repository_blob(p_latest_commit.id, 'iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMNSString+URLArguments.h')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/gitlab/objects.py", line 1521, in repository_blob
raise_error_from_response(r, GitlabGetError)
File "/usr/local/lib/python2.7/site-packages/gitlab/exceptions.py", line 140, in raise_error_from_response
response_body=response.content)
gitlab.exceptions.GitlabGetError: 404: 404 File Not Found
How exactly do I go about escaping the + in python so that I can get the blob of the file?
The solution is to use urllib.quote.
python-gitlab is a superficial wrapper upon the real Gitlab API. The Gitlab API functions on url requests so this solution seemed natural:
rf_blob = p.repository_blob(p_latest_commit.id, urllib.quote('iOSBoilerplate/GooglePlus/GoogleOpenSource.framework/Versions/A/Headers/GTMNSString+URLArguments.h'))

Python 2.7 file read error

I am struggling with file access - I have my python 2.7 here:
C:\Python27
I created a txt file data.txt
and I am trying to get the shell to read and print values from it. Location of data.txt is in the same folder (C:\Python27\data.txt)
Every time I got the same error msg:
Traceback (most recent call last):
File "C:/Python27/FileReader.py", line 1, in <module>
infile = open('data.txt', 'r') # Open the file for reading.
IOError: [Errno 2] No such file or directory: 'data'
You need to enter the full filename data.txt instead of data only:
infile = open('data.txt', 'r')

When i tried to rename a txt file in pythonwin

I wrote a script to rename a txt file in pythonwin as below.
import os
os.rename("northrdge.txt","northrdgechg.txt")
the above text file is in the path:C:\Users\gmayil\Desktop\northrdge.txt.
after this when i run the script i met below error:
Can anyone please help me on this ?
Traceback (most recent call last):
File "C:\Python27\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 433, in ImportFile
exec codeObj in __main__.__dict__
File "<auto import>", line 1, in <module>
File "C:\Python27\Tools\gopi\renamefile.py", line 2, in <module>
os.rename("northrdge.txt","northrdgechg.txt")
WindowsError: [Error 2] The system cannot find the file specified
Your python-file lies in "C:\Python27\Tools\gopi\renamefile.py", but you say your txt-file is in "C:\Users\gmayil\Desktop\". If you pass a relative path, python searches in its own directory, which is "C:\Python27\Tools\gopi\".
So all you have to do is giving the complete path:
os.rename("C:\Users\gmayil\Desktop\northrdge.txt", "C:\Users\gmayil\Desktop\northrdgechg.txt")