在python中擦除文件并移动它

时间:2016-12-18 04:49:22

标签: python-2.7 zip

在脚本中,当我在C:/ Users / User / Desktop / Folder中压缩文件时,它在ZipFile.zip/C:/Users/user/Desktop的结构中显示为zip文件/文件夹而不只是ZipFile.zip/Folder,我无法弄清楚如何解决它。 [压缩代码是第21-26行]

我也试图将创建的zip 文件移动到指定的备份设备[第27行]

我的代码是:

(<*>) = ap

2 个答案:

答案 0 :(得分:0)

你必须为它做一个绝对的路径。

答案 1 :(得分:0)

对于 shutil.move(),您必须提供正确的源和目标路径。

在你的程序中,源路径和文件对象具有相同的名称。所以它调用该对象而不是它应该采用文件的路径。

import os
import sys
import shutil
import zipfile
import traceback

print ('Welcome to USB Backup Utility')
print ('Created by: TheCryptek')
print ('\nWhat directory would you like to back up?')
print ('Example: C:/users/user/Desktop/Folder')
backUp = raw_input('> ') # Files the user specified to back up
print ('\nWhere would you like to back these files up at?')
print ('Example USB Letter: E:/')
backDevice = raw_input('> ') # Device the user specified to save the back up on.
print ('\nName of the zip file you prefer?')
print ('Example: Backup.zip')
backZip = raw_input('> ') # The name of the zip file specified by the user
print ('\nBackup started...')

if not os.path.exists(backDevice + '/BackUp'): # If the BackUp folder doesn't exist on the device then
    os.mkdir(backDevice + 'BackUp') # Make the backup folder on usb device

bkZip = zipfile.ZipFile(backZip, 'w') # Not sure what to say for lines 21 - 26
for dirname, subdirs, files in os.walk(backUp): 
    bkZip.write(dirname)
    for filename in files:
        bkZip.write(os.path.join(dirname, filename))
    bkZip.close()

#print backZip,backDevice
dest = backDevice + '/BackUp'
#print dest
shutil.move(backZip, dest) # Move the zip files created in working directory to the specified back up device -[ Something is wrong with this can't figure out what ]-
print('Backup finished.')