Python script requires permission to copy files - python

I am trying to write a python script to copy some files from one place to another like this:
os.system('cp file/path/a file/path/b')
when I run this within the python prompt it works just fine. But if I put this in a python script it fails with the error
cp: cannot create regular file 'file/path/b': Permission denied
I am getting this error even when I run the script using sudo permissions i.e. sudo ./mysript.py
So what is going on here and is it possible for me to fix this?
EDIT 1:
Following the suggestions of #kungphu and #ShadowRanger I switched from using os.system to using shutil.copy. The problem persists however I am now getting a different error:
Traceback (most recent call last):
File "./myscript.py", line 21, in <module>
shutil.copy('/absolute/file/path/a', '/absolute/file/path/b')
File "/python/lib/python2.6/shutil.py", line 84, in copy
copyfile(src, dst)
File "/python/lib/python2.6/shutil.py", line 50, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: '/absolute/file/path/a'

Related

opening a .txt file in same folder as .py not working

My vscode is having problems using the open() function to open a file.
I am trying to open a .txt file. Both the py file and .txt file are in the same folder (named "practice"), but python terminal is erroring out with
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('ex_text.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'ex_text.txt'
PS C:\Users\liamt\OneDrive\Desktop\VSCode> python -
I tried using the relative path to the .txt file but a permission error came up.
Traceback (most recent call last):
File "c:\Users\liamt\OneDrive\Desktop\VSCode\Practice\p2.py", line 4, in <module>
fin = open('C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\liamt\\OneDrive\\Desktop\\VSCode\\Practice'
PS C:\Users\liamt\OneDrive\Desktop\VSCode>
What am I doing wrong? Really appreciate any help.
When using vscode, open the entire folder/directory containing all the files by using file > open folder, and not just double clicking the python file alone. By opening the entire folder, it is included in the workspace and can be opened by the python file that exists along with it.

How to use runner.py in sumo via command line and solving its common error(permission denied)

When I try to use the runner.py example via the command line, I encounter the following error:
C:\Program Files (x86)\Eclipse\Sumo\doc\tutorial\traci_tls>runner.py
Traceback (most recent call last):
File "C:\Program Files (x86)\Eclipse\Sumo\doc\tutorial\traci_tls\runner.py", line 122, in
generate_routefile()
File "C:\Program Files (x86)\Eclipse\Sumo\doc\tutorial\traci_tls\runner.py", line 47, in generate_routefile
with open("data/cross.rou.xml", "w") as routes:
PermissionError: [Errno 13] Permission denied: 'data/cross.rou.xml'
Who knows how to solve this error?
Regards, Ali
Please make a local copy of the tutorial to your home directory before running it. You probably do not have write access to the installation directory.

cannot write to file if I run .exe

I am writing a short program where I want to write an array full of street names to a .txt file. Everyting works fine, but if I convert the python code to a .exe with pyinstaller, I cant write to a new .txt file anymore. Why does this happen?
with open(f"C:\\Users\\auser\\Desktop\\tset\\{txtname}.txt", "a") as txt_file:
for line in new_addresses:
txt_file.write(line + "\n")
txt_file.close()
this is how I create the .exe:
pyinstaller --onefile streets.py
Traceback (most recent call last):
File "streets.py", line 65, in
PermissionError: [Errno 13] Permission denied: 'C:\Users\auser\Desktop\tset\test.txt'
[7048] Failed to execute script rewriteword
I got this to work now. My anti virus program was blocking for some reason the execution.

Permission error without a file extension

I have an python program that scans all files in a directory, but when I want to open a file with no extension like C:\path\to\file\actual file, then python gives me a permission denied error.How can I fix that?
What I mean with no extension is that it does not have .exe or .txt or something like that.How can I read files without adding extensions to the files manually?
This is the full error:
Traceback (most recent call last):
File "scan.py", line 152, in <module>
file = open(i,'r')
PermissionError: [Errno 13] Permission denied: 'C:\\path\\to\\file\\my_file'
Ok, I have zero idea why it does not work other than what my comment stated, however this program will work for windows.
import os
s = "H:\path\to\file\ActualFile"
for subdir, dirs, files in os.walk(s):
for file in files:
try:
GatheredFiles = (os.path.join(subdir, file))
except PermissionError:
print("You do not have permision to go in here"))
break
Edit: added the try statement

shutil.copy() [Errno 13] Permission Denied

I've been getting an error while trying to use shutil.copy()
What I'm doing is copying a file from one folder to the other, after checking if that file already exists in the destination folder and deleting it:
if os.path.isfile("C:/folder1/file1.csv"):
full_file_name = os.path.join("C:/folder1/", "file1.csv")
if os.path.isfile("C:/folder2/file1.csv"):
os.remove("C:/folder2/file1.csv")
os.chmod("C:/folder2/",0777)
if os.path.isfile(full_file_name):
shutil.copy(full_file_name, "C:/folder2/")
The error I've been getting is:
Traceback (most recent call last):
File "script1.py", line 55, in <module>
File "shutil.pyc", line 119, in copy
File "shutil.pyc", line 82, in copyfile
IOError: [Errno 13] Permission denied: 'C:/folder1/file1.csv'
What could the problem be?
The important part is:
Permission denied: 'C:/folder1/file1.csv'
Which means it cannot access file1.csv. Check this file is readable by the user running the script.

Categories

Resources