![](/img/trans.png)
[英]python Pyinstaller IOError: [Errno 13] Permission denied
[英]Python IOError: Errno 13 Permission denied
好吧,我完全困惑。 我整夜都在工作,但無法正常工作。 我很樂意查看文件,我要做的就是讀取該死的東西。 每次嘗試,我都會得到:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
scan('test', rules, 0)
File "C:\Python32\PythonStuff\csc242hw7\csc242hw7.py", line 45, in scan
files = open(n, 'r')
IOError: [Errno 13] Permission denied: 'test\\test'
這是我的代碼。 它尚未完成,但我認為我至少應該為要測試的部分獲取正確的值。 基本上,我想查看一個文件夾,如果有文件掃描,它將查找我設置signatures
的內容。 如果有文件夾,我會或不會掃描它們,具體取決於指定的depth
。 如果depth < 0
,它將返回。 如果depth == 0
那么它將僅掃描第一個文件夾中的元素。 如果depth > 0
,它將掃描文件夾直到指定深度。 這些都沒關系,因為無論出於何種原因,我都沒有讀取文件的權限。 我不知道我在做什么錯。
def scan(pathname, signatures, depth):
'''Recusively scans all the files contained in the folder pathname up
until the specificed depth'''
# Reconstruct this!
if depth < 0:
return
elif depth == 0:
for item in os.listdir(pathname):
n = os.path.join(pathname, item)
try:
# List a directory on n
scan(n, signatures, depth)
except:
# Do what you should for a file
files = open(n, 'r')
text = file.read()
for virus in signatures:
if text.find(signatures[virus]) > 0:
print('{}, found virus {}'.format(n, virus))
files.close()
只需快速編輯:
下面的代碼做了非常相似的事情,但是我無法控制深度。 但是,它工作正常。
def oldscan(pathname, signatures):
'''recursively scans all files contained, directly or
indirectly, in the folder pathname'''
for item in os.listdir(pathname):
n = os.path.join(pathname, item)
try:
oldscan(n, signatures)
except:
f = open(n, 'r')
s = f.read()
for virus in signatures:
if s.find(signatures[virus]) > 0:
print('{}, found virus {}'.format(n,virus))
f.close()
我敢猜測test\\test
是一個目錄,並且發生了一些異常。 您盲目地捕獲了異常,然后嘗試將目錄作為文件打開。 這樣就可以在Windows上使用Errno 13。
使用os.path.isdir
區分文件和目錄,而不是try ... except。
for item in os.listdir(pathname):
n = os.path.join(pathname, item)
if os.path.isdir(n):
# List a directory on n
scan(n, signatures, depth)
else:
# Do what you should for a file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.