IOError:[Errno 13]权限被拒绝,同时打开一个文件 [英] IOError: [Errno 13] Permission denied, While opening a file
问题描述
我有一个Python脚本,用于查找文件夹中的任何更改(添加/删除)。
我正在寻找任何新的文件添加,并希望打开它们,并从它们读取一些数据。
代码是
#用于检查新作业的实用程序
导入操作系统
导入win32file
导入win32event
import win32con
import string
path = rC:\Users\dZONE\Desktop\py
change_handle = win32file.FindFirstChangeNotification(path ,0,win32con.FILE_NOTIFY_CHANGE_FILE_NAME)
try:
old_path_contents = dict([(f,None)for os.listdir(path)])
while 1:
result = win32event.WaitForSingleObject(change_handle,500)
如果结果== win32con.WAIT_OBJECT_0:
new_path_contents = dict([(f,None )for f in os.listdir(path)])
added = [f for new_path_contents if not in old_path_contents]
#deleted = [f for f in old_path_contents if if not in new_path_contents]
如果添加:printadded:,,.join(added)
#if删除:打印已删除:,,.join(已删除)
#打开
打印Len,len(已添加)
添加项目:
打印item
ad = open(item,'r')
print ad.read()
old_path_contents = new_path_contents
win32file.FindNextChangeNotification(change_handle)
finally:
win32file.FindCloseChangeNotification(change_handle)
当我试图打开它给我的IO错误13的文件。虽然我有文件夹和文件的权利。我写了另一个测试代码,并尝试直接打开文件名打开(),它确实工作。
如果任何人都可以指出我正在做的错误
编辑:错误信息
Traceback最近一次调用最后一次):
文件C:\ Users \DZONE\Desktop\py\util.py,第50行,在< module>
ad = open(item,'r')
IOError:[Errno 13] Permission denied:'ad.sjs_hdr'
PS我是新来的python,所以也许我犯了一些非常愚蠢的错误
谢谢
路径
命名的目录中。您必须使用 os.path.join
function 把路径
和项目
放在一起: <$ p $
print item
fullfilename = os.path.join(path,item)
ad = open(fullfilename,'r')
print ad.read()
I have a Python script which looks for any changes in a folder(addition/deletion) of files. I am looking for any new files added and wants to open them and read some data from them.
The code is
# Utility to check for any new jobs
import os
import win32file
import win32event
import win32con
import string
path= r"C:\Users\dZONE\Desktop\py"
change_handle = win32file.FindFirstChangeNotification (path,0,win32con.FILE_NOTIFY_CHANGE_FILE_NAME)
try:
old_path_contents = dict ([(f, None) for f in os.listdir (path)])
while 1:
result = win32event.WaitForSingleObject (change_handle, 500)
if result == win32con.WAIT_OBJECT_0:
new_path_contents = dict ([(f, None) for f in os.listdir (path)])
added = [f for f in new_path_contents if not f in old_path_contents]
#deleted = [f for f in old_path_contents if not f in new_path_contents]
if added: print "Added: ", ", ".join (added)
#if deleted: print "Deleted: ", ", ".join (deleted)
# My open
print "Len" , len(added)
for item in added:
print item
ad=open(item,'r')
print ad.read()
old_path_contents = new_path_contents
win32file.FindNextChangeNotification (change_handle)
finally:
win32file.FindCloseChangeNotification (change_handle)
When i am trying to open the file it is giving me the IO Error 13. Though i have rights on the folder and the file. I wrote another test code and tried opening the same file directly giving the file name to open() and it did worked.
If anyone can point out the mistake i am doing would be really appreciated.
EDIT: Error message
Traceback (most recent call last):
File "C:\Users\dZONE\Desktop\py\util.py", line 50, in <module>
ad=open(item,'r')
IOError: [Errno 13] Permission denied: 'ad.sjs_hdr'
P.S I am new to python so maybe i am making some really silly mistake thanks
The .listdir()
method gives you filenames without paths only, you are probably trying to open a file in the current directory, not in the directory named by path
.
You have to use the os.path.join
function to put path
and item
together:
for item in added:
print item
fullfilename = os.path.join(path, item)
ad=open(fullfilename,'r')
print ad.read()
这篇关于IOError:[Errno 13]权限被拒绝,同时打开一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!