[英]Using PySpark in Python: WindowsError: [Error 5] Access is denied
[英]Python WindowsError 5 Access Denied
我創建了此腳本,該腳本將為我提供給定目錄下所有文件的html文件,並且設法使它正確。 除了一件事...僅在Windows上,當我嘗試通過C:/目錄進行訪問時,會遇到WindowsError 5 Access Deni。
這是我的應用程序的源代碼:
import os
import hashlib
import platform
import sys
import argparse
import HTML
class Map(object):
def __init__(self,param):
self.param_list = param
self.slash = self.slash_by_os()
self.result_list = []
self.os = ""
def calc_md5(self,file_path):
hash = hashlib.md5()
with open(file_path, 'rb') as file_to_check:
for chunk in iter(lambda: file_to_check.read(4096), ''):
hash.update(chunk)
return hash.hexdigest()
def slash_by_os(self):
general_id = platform.system()
actual_os = ""
if general_id == "Darwin" or general_id == "darwin":
actual_os = "UNIX"
elif general_id == "Linux" or general_id == "linux":
actual_os = "UNIX"
elif general_id == "SunOS":
actual_os = "UNIX"
elif general_id == "Windows" or general_id == "windows":
actual_os = "WIN"
else:
actual_os = general_id
if actual_os == "UNIX":
return '/'
elif actual_os == "WIN":
return '\\'
else:
return '/'
self.os = actual_os
def what_to_do(self,new_dir):
act = []
act.append(new_dir[:-1])
for param in self.param_list:
if param == "md5":
x = self.calc_md5(new_dir[:-1])
act.append(x)
elif param == "size":
x = os.stat(new_dir[:-1]).st_size
act.append(x)
elif param == "access":
x = os.stat(new_dir[:-1]).st_atime
act.append(x)
elif param == "modify":
x = os.stat(new_dir[:-1]).st_mtime
act.append(x)
elif param == "creation":
x = os.stat(new_dir[:-1]).st_ctime
act.append(x)
return act
def list_of_files(self ,dir_name ,traversed = [], results = []):
dirs = os.listdir(dir_name)
if dirs:
for f in dirs:
new_dir = dir_name + f + self.slash
if os.path.isdir(new_dir) and new_dir not in traversed:
traversed.append(new_dir)
self.list_of_files(new_dir, traversed, results)
else:
act = self.what_to_do(new_dir)
results.append(act)
self.result_list = results
return results
def parse_args():
desc = "Welcom To dirmap.py 1.0 \n Created DD 2013 RT"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-p','--path', help='Path To Original Directory', required=True)
parser.add_argument('-md','--md5', action = 'store_true',help='Show md5 hash of file', required=False)
parser.add_argument('-s','--size', action = 'store_true', help='Show size of file', required=False)
parser.add_argument('-a','--access', action = 'store_true', help='Show access time of file', required=False)
parser.add_argument('-m','--modify', action = 'store_true', help='Show modification time of file', required=False)
parser.add_argument('-c','--creation', action = 'store_true', help='Show creation of file', required=False)
args = vars(parser.parse_args())
params = []
for key,value in args.iteritems():
if value == True:
params.append(key)
return args,params
def main():
args , params = parse_args()
dir_path = args['path']
map = Map(params)
dir_list = map.list_of_files(dir_path)
params.insert(0,"path")
htmlcode_dir = HTML.table(dir_list,header_row=params)
print htmlcode_dir
main()
我讀到很多有關此錯誤的信息,但仍然無法解決。 這是回溯:
C:\\ Users \\ dd> C:\\ Users \\ dd \\ Desktop \\ dirmap.py -p C:\\ Traceback(最近一次調用):文件“ C:\\ Users \\ dd \\ Desktop \\ dirmap.py”,第136行,在main()文件“ C:\\ Users \\ dd \\ Desktop \\ dirmap.py”的第124行中,在主dir_list = map.list_of_files(dir_path)文件“ C:\\ Users \\ dd \\ Desktop \\ dirmap.py”中, list_of_files self.list_of_files中的第87行(遍歷新結果,遍歷結果)File“ C:\\ Users \\ dd \\ Desktop \\ dirmap.py”,list_of_files中的第81行dirs = os.listdir(dir_name)WindowsError:[錯誤5]訪問被拒絕:“ C:/ Documents and Settings \\ *。*”
有幫助嗎? 提前致謝...
謝謝所有人,但我發現我的問題只是權限問題。 到達C:\\時,我需要成為管理員,但不是。 解決了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.