我想压缩当前文件夹时出现PermissionError

时间:2018-11-07 14:44:43

标签: python path permissions

我正在研究AI Sweigart的“使用python自动化无聊的东西”项目9.5。

程序将当前文件夹备份到ZIP文件中。

我已经完成了编码,当我通过Terminator运行.py文件时,它可以正常工作。

这是我的代码:

#!/usr/bin/env python3
# backupToZip.py - backup whole folder into a ZIP file whose name increments

import zipfile, os

def backupToZip(folder):
    folder = os.path.abspath(folder) #make sure it's abspath

    # Figure out filename
    number = 1
    while True:
        zipFilename = os.path.basename(folder) + '_' + str(number) + '.zip'
        if not os.path.exists(zipFilename):
            break
        number = number + 1

    # create the ZIP file
    print('Creating %s...' % (zipFilename))
    backupZip = zipfile.ZipFile(zipFilename, 'w')

    # walk the entire folder tree and compress the files in each folder.
    for foldername, subfolders, filenames in os.walk(folder):
        print('Adding files in %s...' % (foldername))
        # add the current folder to the ZIP file
        backupZip.write(foldername)
        for filename in filenames:
            newBase = os.path.basename(folder) + '_'
            if filename.startswith(newBase) and filename.endswith('.zip'):
                continue # don't backup the backup ZIP files
            backupZip.write(os.path.join(foldername, filename))
    backupZip.close()
    print('Done')

backupToZip('.')

然后,我想使其更易于使用-通过双击文件来运行该文件。

然后我将文件名从backupToZip.py更改为backupToZip.py.command。 在Terminator中使用“ chmod + x backupToZip.py.command”授权文件。

然后我尝试双击该文件,但弹出错误:

Last login: Wed Nov  7 22:33:19 on ttys001
huxiaorendeMacBook-Pro-2:~ huxiaoren$ /Users/huxiaoren/python/automate/project_9.5_backupToZip/backupToZip.py.command ; exit;
Creating huxiaoren_5.zip...
Adding files in /Users/huxiaoren...
Traceback (most recent call last):
  File "/Users/huxiaoren/python/automate/project_9.5_backupToZip/backupToZip.py.command", line 34, in <module>
    backupToZip('.')
  File "/Users/huxiaoren/python/automate/project_9.5_backupToZip/backupToZip.py.command", line 30, in backupToZip
    backupZip.write(os.path.join(foldername, filename))
  File "/Users/huxiaoren/miniconda3/lib/python3.7/zipfile.py", line 1721, in write
    with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
PermissionError: [Errno 13] Permission denied: '/Users/huxiaoren/.rnd'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

我意识到也许程序无法识别带有'。'的当前文件夹路径。然后,我将代码的最后一行更改为:

backupToZip('.')

收件人:

folder = os.getcwd()
backupToZip(folder)

再次弹出权限错误:

Last login: Wed Nov  7 22:35:55 on ttys001
huxiaorendeMacBook-Pro-2:~ huxiaoren$ /Users/huxiaoren/python/automate/project_9.5_backupToZip/backupToZip.py.command ; exit;
Creating huxiaoren_7.zip...
Adding files in /Users/huxiaoren...
Traceback (most recent call last):
  File "/Users/huxiaoren/python/automate/project_9.5_backupToZip/backupToZip.py.command", line 35, in <module>
    backupToZip(folder)
  File "/Users/huxiaoren/python/automate/project_9.5_backupToZip/backupToZip.py.command", line 30, in backupToZip
    backupZip.write(os.path.join(foldername, filename))
  File "/Users/huxiaoren/miniconda3/lib/python3.7/zipfile.py", line 1721, in write
    with open(filename, "rb") as src, self.open(zinfo, 'w') as dest:
PermissionError: [Errno 13] Permission denied: '/Users/huxiaoren/.rnd'
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

我如何找出问题出在哪里?

0 个答案:

没有答案