0

私は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) バックアップに失敗しました

4

2 に答える 2

0

あなたが見ているコード例は、完全に Windows 固有のものであり、C:\blahパスが含まれています。OS X での同等のパスは次のようになります。

/Users/yourusername/Documents

'(また、コードを変換するときに、いくつかの s をs に何らかの形で変換したよう,です。それが何であるかはわかりません。)

于 2013-01-16T02:13:54.670 に答える
0

別の回答者が指摘したように、使用しているコードは Windows 固有のものであり、OSX で機能させるには、他の回答者が提案した修正が必要になります。

問題を診断するために、次のようにコードにいくつかの修正を加えました(一部のコンマをアポストロフィに変更し、strftime()呼び出しを変更し、print()実際に何が入っているかを示すためにa を追加しましたzip_command):

import os
import time
source = ['C:\\learningPython','C:\\scan']
target_dir = 'C:/backup'
target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.zip'   # modified
zip_command = "zip -qr {0} {1}".format(target, ' '.join(source))
print(zip_command)  # added - see discussion below
if os.system(zip_command) == 0:
    print('Successful backup to', target)
else:
    print('Backup FAILED')

また、3 つのフォルダーを作成しc:\backup、ファイルをc:\learningPython配置して何かを行うと、修正されたスクリプトにより、Windows PC で次の出力が得られました。c:\scanc:\learningPythonzip

C:\usr\sjl\dev\test\python>python ziptest.py
zip -qr C:/backup\20130116152602.zip C:\learningPython C:\scan
Successful backup to C:/backup\20130116152602.zip

...それは、あなたが望んでいたことだと思います。

Python からシステム コマンドを実行しているときは、コマンドos.system()を実行するように要求したとおりに、コマンドを出力するのに常にお金がかかることがわかりました。シェルから手動で実行する必要があると思われることを実行しない場合)。

元のバージョンでは、「Y」の後に大文字の「M」があり (月数strftime()ではなく分数を示していた)、「 d' は日番号です。strftime()'D' は、フォーマット文字列の有効なコード文字ではありません(こちらを参照してください)。私の場合、Python 3.2 を使用して、スクリプトを実行しようとしたときに間違った文字が原因でランタイム エラーが発生しました。

あなたの他の質問に関しては、処理を求められているsourceディレクトリのリストです。zip1 つのディレクトリのみを処理する必要がある場合は、as で始まる行zip_commandを (今回は OS X / Unix スタイルのフォルダー名で)簡単に記述できます。

zip_command = "zip -qr {0} {1}".format(target, '/Users/yourusername/Documents'))
于 2013-01-16T02:48:43.103 に答える