私はPythonをプログラミングするのが初めてで(現在5日)、このフォーラムで答えが見つからないように見える問題に遭遇しました。詳細な説明をいただければ幸いです。
存在すること。Macbook Proでpython 3を使用しています。「komodo edit」というエディタを使っています CH Swaroop の「Byte of Python」を参考にしています。
私が抱えている問題は、あるフォルダーからファイルを取得し、zip ファイルとして別のフォルダーにバックアップするプログラムを作成する例です。
本の例は次のとおりです。
Save as backup_ver1.py:
import os
import time
# The files and directories to be backed up are specified in a list.
source = ['"C:\\My Documents"', 'C:\\Code']
# Notice we had to use double quotes inside the string for names with spaces in it.
# The backup must be stored in a main backup directory
target_dir = 'E:\\Backup' # Remember to change this to what you will be using
# The files are backed up into a zip file.
# The name of the zip archive is the current date and time
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'
# We use the zip command to put the files in a zip archive
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
# Run the backup
if os.system(zip_command) == 0:
print('Successful backup to', target)
else:
print('Backup FAILED')
私のコードは次のとおりです
import os
import time
source = ['C:\\learningPython','C:\\scan'] # can i use / instead of the \?
# why is square brackets used here? i'm under the assumption the square brackets are used for list. i'm also assuming one is the directory and the other is the file.
target_dir = 'C:/backup' # created a "backup" folder
target = target_dir + os.sep +time.strftime('%Y%M%D%H%M%S') + '.zip'
zip_command = "zip -qr {0} {1}".format(target, ‚ ‚.join(source))
if os.system(zip_command) == 0:
print(‚Successful backup to‚, target)
else:
print(‚Backup FAILED‚)
私は得る
zip エラー: 何もする必要はありません! (試行: zip -qr C:/backup/20133201/15/13203219.zip . -i C:learningPythonC:scan) バックアップに失敗しました