windows - PermissionError: [Errno 13] Permission denied when accessing Apache log file - python

Here is my code, which intends to constantly check apache log file and process the newly updated contents.
# detect.py
fp = open(r'C:\xampp\apache\logs\access.log', 'r')
...
def follow(fp):
fp.seek(0, 2)
while True:
line = fp.readline()
if not line:
time.sleep(0.0001)
continue
yield line
...
if __name__ == '__main__':
loglines = follow(fp)
for line in loglines:
data = LogParser(line, db)
...
When I run this file, I always get the following error:
Traceback (most recent call last):
File ".\core\detect.py", line 28, in <module>
for line in loglines:
File ".\core\detect.py", line 19, in follow
line = fp.readline()
PermissionError: [Errno 13] Permission denied
I have tried nearly all available means, such as: changing file privileges, running as administrators, killing all python related process before running this file ...
but I still get that error. I want to know how to deal with this problem.

Related

PermissionError: [Errno 13] Permission denied: Python

I have a script that is called from DJango, but it doesn't execute instead it gives PermissionError: [Errno 13] Permission denied. I'm using CMD on Windows as admin.
Here is how I'm calling:
cmd = os.path.join(os.getcwd(), "D:/commerceTest/cs50-commerceM/Photo/GFPGAN/inference_gfpgan.py --upscale 2 --model_path D:/commerceTest/cs50-commerceM/Photo/GFPGAN/experiments/pretrained_models/ --test_path inputs/whole_imgs/GFPGANCleanv1-NoCE-C2.pth --save_root D:/commerceTest/cs50-commerceM/Photo/GFPGAN/results")
#os.system("python D:/commerceTest/cs50-commerceM/Photo/GFPGAN/inference_gfpgan.py --upscale 2 --model_path D:/commerceTest/cs50-commerceM/Photo/GFPGAN/experiments/pretrained_models/ --test_path inputs/whole_imgs/GFPGANCleanv1-NoCE-C2.pth --save_root D:/commerceTest/cs50-commerceM/Photo/GFPGAN/results")
os.system('{} {}'.format('python', cmd))
Here is the message I received:
Traceback (most recent call last):
File "D:\commerceTest\cs50-commerceM\Photo\GFPGAN\inference_gfpgan.py", line 98, in <module>
main()
File "D:\commerceTest\cs50-commerceM\Photo\GFPGAN\inference_gfpgan.py", line 52, in main
restorer = GFPGANer(
File "D:\commerceTest\cs50-commerceM\Photo\GFPGAN\gfpgan\utils.py", line 60, in __init__
loadnet = torch.load(model_path)
File "D:\Users\rapha\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\serialization.py", line 594, in load
with _open_file_like(f, 'rb') as opened_file:
File "D:\Users\rapha\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\serialization.py", line 230, in _open_file_like
return _open_file(name_or_buffer, mode)
File "D:\Users\rapha\AppData\Local\Programs\Python\Python39\lib\site-packages\torch\serialization.py", line 211, in __init__
super(_open_file, self).__init__(open(name, mode))
PermissionError: [Errno 13] Permission denied: 'D:/commerceTest/cs50-commerceM/Photo/GFPGAN/experiments/pretrained_models/'
I can only guess here but the error that you are getting is because Python is trying to open a directory as a file (which won't work).
It looks like the path that you are passing in to --model_path is a directory but torch.load() expects a file, that would explain it.
I would start with understanding what exactly the script inference_gfpgan.py does and how it works.

Failing to make an array of Images in Python

I am trying to create an array of .jpg files, but the compiler is not building the array
More specifically, my problem is that a public folder, whose path is defined as the object path, is not accessible by my Python compiler [Spyder]. However, the folder, and its respective files are all public and open access to everyone. What might be the reason that my computer cannot access the images?
Code 1 is an simple function to find and access the file path I want, and the Kernal results show what is failing.
Code 2 is the syntax for the isolated error in the program I am applying the open() method. Kernal results depict compiler failure.
Code 1:
import os
path = r'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
try:
os.path.exists(path)
if (True):
R = open(path)
R.close()
except FileNotFoundError:
print("file does not exist")
Kernal for Code 1:
!runfile('C:/Users/BeckerLab/untitled6.py', wdir='C:/Users/BeckerLab')
Traceback (most recent call last):
File "C:\Users\BeckerLab\untitled6.py", line 8, in <module>
R = open(path)
PermissionError: [Errno 13] Permission denied: 'C:/Users/BeckerLab/Pictures/Final_Sample_Set/Right2'
Code 2:
import os
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
Kernal Results for Code 2:
!runfile('C:/Users/BeckerLab/almost.py', wdir='C:/Users/BeckerLab')
2020-04-05 12:59:28
Traceback (most recent call last):
File "C:\Users\BeckerLab\almost.py", line 46, in <module>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
File "C:\Users\BeckerLab\almost.py", line 46, in <listcomp>
rightSamples = [open(file, 'r+') for file in os.listdir(r'C:/Users/Public/Right2')]
FileNotFoundError: [Errno 2] No such file or directory: 'R1.JPG'
Notice that your condition is:
os.path.exists(path)
if (True):
which will always be true. Maybe try:
if (os.path.exists(path)):
Try moving the files to another directory like 'D:/../BeckerLab/untitled6.py'

Downloading a file but getting permission errors

I'm trying to download a file but when it's trying to write to the current directory it gives a permission error
Traceback (most recent call last):
File "C:\Users\HP User\Desktop\WWE Tool\MasterDownload.py", line 22, in <module>
with open(x, 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\HP User\\Desktop\\WWE Tool'
Code:
MasterDownload = requests.get(url=Master, headers=Heads)
fpath = os.getcwd()
with open(fpath, 'wb') as f:
f.write(MasterDownload.content)
I checked the current path and eveything looks fine, I just can't get around as to why it's not writing as I am an admin
You're actually trying to write to a directory (the process' current working directory - as obtained from os.getcwd()), not to a file. Try selecting an actual file in that directory to write to instead of the directory itself, and the issue might go away.

IOError: [Errno 13] Permission denied in python when i read a file

i have a text file in an external HD E:
my_file = "E:\\myfolder\\myfile.txt"
lines_len = 0
for p in open(my_file , "r"):
lines_len +=1
i wish to read all line. When the file read a certain amount of line i get this message
Traceback (most recent call last):
File "<input>", line 2, in <module>
IOError: [Errno 13] Permission denied

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.

Categories