Errno 13 权限被拒绝:'tmp\\csv.gz
Errno 13 Permission denied: 'tmp\\csv.gz
在我的 python 脚本中,我正在创建一个 tmp 文件夹来处理 csv.gz 文件夹的提取。
部分代码:
try:
shutil.rmtree("tmp") # dangerous command removes all contents of tmp folder
except OSError as ex:
traceback.print_exc()
logmsg = "Folder already deleted or not present 'tmp'"
instalog.appendMessage('WARNING', logmsg)
if not os.path.exists('tmp'):
original_umask = os.umask(0)
desired_permission =0o755
os.makedirs('tmp', desired_permission)
os.umask(original_umask)
我不知道 umask 或 desire permission 是否在做某事,因为我在深入研究了这个问题后插入了它,Permission denied 错误只是偶尔发生,所以我想我可以把它去掉广告仍然是同样,有时会拒绝我的许可,有时不会。因此,创建了 tmp。这将是我的下载目录,脚本运行时将成为 tmp 文件夹:
download_dir = os.path.dirname(os.path.abspath(__file__)) + "\tmp"
现在是代码给出错误的地方:
newfile_path = 'Todas as Ações Executadas - Outsourcer.csv'
## Download to folder and use whatever comes out
file_downloaded = False
while not file_downloaded:
for file in os.listdir("tmp"):
if file.endswith("csv.gz"):
fp = os.path.join('tmp', file)
logmsg = ("Extracting ", fp)
instalog.appendMessage('INFO', logmsg)
with gzip.open(fp, 'r') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
elif file.endswith(".csv"):
fp = os.path.join('tmp', file)
logmsg = ("File was not compressed ", fp)
instalog.appendMessage('INFO', logmsg)
with open(fp, 'rb') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
我是 运行 Windows 中的脚本,有时错误只是简单地消失了,但我需要它消失。
我将在 powershell 中提供文件夹的权限,如下图所示:
import os
import shutil
import time
import gzip
newfile_path = 'Todas as Ações Executadas - Outsourcer.csv'
## Download to folder and use whatever comes out
file_downloaded = False
while not file_downloaded:
for file in os.listdir("tmp"):
if file.endswith("csv.gz"):
fname = os.path.join('tmp', file)
logmsg = ("Extracting ", fname)
instalog.appendMessage('INFO', logmsg)
try:
with gzip.open(fname) as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
break
except OSError:
time.sleep(1)
elif file.endswith(".csv"):
fname = os.path.join('tmp', file)
logmsg = ("File was not compressed ", fname)
instalog.appendMessage('INFO', logmsg)
shutil.copy(fname, newfile_path)
file_downloaded = True
break
备注:
- 我已将
fp
重命名为 fname
,因为 fp
通常被理解为 file pointer
,而它是文件名或路径
gzip.open
默认以二进制模式打开。因此,解压缩的文件也被写为二进制文件。我不确定这对 Windows 下的 CSV 文件的效果如何,因为 CSV 文件是文本文件。
- 我已将
elif
子句中的 shutil.copyfileobj
替换为简单的 shutil.copy
elif
子句没有 try-except
子句。这可能意味着如果只下载了部分未压缩的文件,则可能会复制该文件。无法检查文件是否已完全下载,除非您事先知道文件大小(并对其进行测试)。所以这可能会导致只复制部分文件。
在我的 python 脚本中,我正在创建一个 tmp 文件夹来处理 csv.gz 文件夹的提取。 部分代码:
try:
shutil.rmtree("tmp") # dangerous command removes all contents of tmp folder
except OSError as ex:
traceback.print_exc()
logmsg = "Folder already deleted or not present 'tmp'"
instalog.appendMessage('WARNING', logmsg)
if not os.path.exists('tmp'):
original_umask = os.umask(0)
desired_permission =0o755
os.makedirs('tmp', desired_permission)
os.umask(original_umask)
我不知道 umask 或 desire permission 是否在做某事,因为我在深入研究了这个问题后插入了它,Permission denied 错误只是偶尔发生,所以我想我可以把它去掉广告仍然是同样,有时会拒绝我的许可,有时不会。因此,创建了 tmp。这将是我的下载目录,脚本运行时将成为 tmp 文件夹:
download_dir = os.path.dirname(os.path.abspath(__file__)) + "\tmp"
现在是代码给出错误的地方:
newfile_path = 'Todas as Ações Executadas - Outsourcer.csv'
## Download to folder and use whatever comes out
file_downloaded = False
while not file_downloaded:
for file in os.listdir("tmp"):
if file.endswith("csv.gz"):
fp = os.path.join('tmp', file)
logmsg = ("Extracting ", fp)
instalog.appendMessage('INFO', logmsg)
with gzip.open(fp, 'r') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
elif file.endswith(".csv"):
fp = os.path.join('tmp', file)
logmsg = ("File was not compressed ", fp)
instalog.appendMessage('INFO', logmsg)
with open(fp, 'rb') as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
我是 运行 Windows 中的脚本,有时错误只是简单地消失了,但我需要它消失。
我将在 powershell 中提供文件夹的权限,如下图所示:
import os
import shutil
import time
import gzip
newfile_path = 'Todas as Ações Executadas - Outsourcer.csv'
## Download to folder and use whatever comes out
file_downloaded = False
while not file_downloaded:
for file in os.listdir("tmp"):
if file.endswith("csv.gz"):
fname = os.path.join('tmp', file)
logmsg = ("Extracting ", fname)
instalog.appendMessage('INFO', logmsg)
try:
with gzip.open(fname) as f_in:
with open(newfile_path, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
file_downloaded = True
break
except OSError:
time.sleep(1)
elif file.endswith(".csv"):
fname = os.path.join('tmp', file)
logmsg = ("File was not compressed ", fname)
instalog.appendMessage('INFO', logmsg)
shutil.copy(fname, newfile_path)
file_downloaded = True
break
备注:
- 我已将
fp
重命名为fname
,因为fp
通常被理解为file pointer
,而它是文件名或路径 gzip.open
默认以二进制模式打开。因此,解压缩的文件也被写为二进制文件。我不确定这对 Windows 下的 CSV 文件的效果如何,因为 CSV 文件是文本文件。- 我已将
elif
子句中的shutil.copyfileobj
替换为简单的shutil.copy
elif
子句没有try-except
子句。这可能意味着如果只下载了部分未压缩的文件,则可能会复制该文件。无法检查文件是否已完全下载,除非您事先知道文件大小(并对其进行测试)。所以这可能会导致只复制部分文件。