I have a script on a RHEL 7.x machine written in Python3. In testing this script I created a function which will append to a text file in the same directory.
If I execute the script from the local directory ie - ./pyscript.py everything works as expected.
But I am trying to execute this from a Bash script a couple directories higher and it doesn't seem to work right. The other functions in the script will execute, but this very last one which appends to a text file will not.
Now, if I run the script as the user which owns it(and the txt file) from my home dir, the script errors out with a permission error. BUT if I run the script with sudo it finishes with NO error, However it does NOT write to the text file.
My user has RW privileges on every dir between the bash script and the python script.
Any thoughts on why a sudo or local user run doesn't seem to let me write to the text file??
Edit
Traceback (most recent call last):
File "ace/ppod/my_venv/emergingThreats/et_pro_watchlists.py", line 165, in <module>
with open('etProLog.txt', 'a') as outlog:
PermissionError: [Errno 13] Permission denied: 'etProLog.txt'
If you use open("filename.txt", 'mode'), it will open that file in the directory from which the script is executed, not relative to the current directory of the script. If you want the path to directory where the script exists, import the os module and use open(os.path.dirname(os.path.abspath(__file__))+"filename.txt"). The permission error is because the file doesn't exist; sudo overrides that but does nothing because the file doesn't exist.
When I run the following code I get an error named permission denied:
f=open('C:/Windows/System32/azm.txt','w')
f.write('+989193667998')
f.close()
On Windows, use backslashes instead of slashes to specify file paths:
f = open(r'c:\windows\system32\azm.txt', 'w')
And since you're trying to write to a system directory, make sure you run your code as Administrator.
The following is my file deletion code:
path = settings.MEDIA_ROOT
os.remove(os.path.join(path, file_name))
When run I get the following HTTP 500 error:
WindowsError at /project/delete_files/
[Error 2] The system cannot find the file specified: u'C:\DjangoEmt/static/uploads/bridge.jpg'
I have checked the file exists in the directory. Can someone please help me as to why its not working.
Sidenote: I am using ajax in django if this makes any difference.
I see in your file path error, a combination of slash and backslash.
replace '/' to '\' if your application run on windows system.
path = settings.MEDIA_ROOT
os.remove(os.path.join(path, file_name.replace('/', '\')))
When we try to execute listdir on Solaris, python throws exception because lost+found, which is system folder inside directory where we run listdir, cannot be accessed.
instanceDirs = listdir(baseDir)
OSError: [Errno 13] Permission denied: '/some path/lost+found'
How we can bypass this problem and return a list of all files and directories to which we have permissions?
I found a solution: with try except pass it ignores exception and always returns full list of files and dirs.
I want to create a file and write some integer data to it in python. For example, I have a variable abc = 3 and I am trying to write it to a file (which doesn't exist and I assume python will create it on its own):
fout = open("newfile.dat", "w")
fout.write(abc)
First, will python create a newfile.dat on its own? Secondly, it's giving me this error:
IOError: [Errno 13] Permission denied: 'newfile.dat'
What's wrong here?
Please close the file if its still open on your computer, then try running the python code.
I hope it works
This also happens when you attempt to create a file with the same name as a directory:
import os
conflict = 'conflict'
# Create a directory with a given name
try:
os.makedirs(conflict)
except OSError:
if not os.path.isdir(conflict):
raise
# Attempt to create a file with the same name
file = open(conflict, 'w+')
Result:
IOError: [Errno 13] Permission denied: 'conflict'
I've had the same issue using the cmd (windows command line) like this
C:\Windows\System32> "G:\my folder\myProgram.py"
Where inside the python file something like this
myfile = open('myOutput.txt', 'w')
The error was that when you don't use a full path, python would use your current directory, and because the default directory on cmd is
C:\Windows\System32
that won't work, as it seems to be write-protected and needs permission & confirmation form an administrator
Instead, you should use full paths, for example:
myfile = open('G:\my folder\myOutput.txt', 'w')
Permission denied simply means the system is not having permission to write the file to that folder. Give permissions to the folder using "sudo chmod 777 " from terminal and try to run it. It worked for me.
I write python script with IDLE3.8(python 3.8.0)
I have solved this question:
if the path is
shelve.open('C:\\database.dat')
it will be
PermissionError: [Errno 13] Permission denied: 'C:\\database.dat.dat'.
But when I test to set the path as
shelve.open('E:\\database.dat')
That is OK!!!
Then I test all the drive(such as C,D,F...) on my computer,Only when the Path set in Disk
C:\\
will get the permission denied error.
So I think this is a protect path in windows to avoid python script to change or read files in system Disk(Disk C)
In order to write on a file by using a Python script, you would have to create a text file first.
Example A file such as C:/logs/logs.txt should exist.
Only then the following code works:
logfile=open(r"C:/logs/logs.txt",'w')
So summary.
A text file should exist on the specified location
Make sure you close the file before running the Python script.
To answer your first question: yes, if the file is not there Python will create it.
Secondly, the user (yourself) running the python script doesn't have write privileges to create a file in the directory.
If you are executing the python script via terminal pass --user to provide admin permissions.
Worked for me!
If you are using windows run the file as admin.
If you are executing via cmd, run cmd as admin and execute the python script.
Make sure that you have write permissions for that directory you were trying to create file by properties