2016-05-14 7 views
0

私はzipファイル用のディレクトリを作成してバックアップを保存するプログラムを作成しようとしています:これはエクササイズですPythonのバイト(私はあなたたちは、彼が起こっている場所を確認できるように、完全な例を与えるつもりです。) サンプルコードは次のとおりです。makedirs()はAttributeErrorを返します: 'int'オブジェクトに 'rfind'属性がありません

#! /usr/bin/env python3 
    import os 
    import time 


    # 1. The files and directories to be backed up are specified in a list. 
    source = ['~/Desktop/python'] 

    # 2. The backup must be stored in a main backup directory 
    target_dir = '~/Dropbox/Backup/' # Remember to change this to what you'll be using 

    # 3. The files are backed up into a zip file. 
    # 4. 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' 
    now = time.strftime('%H%M%S') 

    # Create the subdirectory if it isn't already there. 
    if not os.path.exists(today): 
     os.mkdir(today) # make directory 
     print('Successfully created directory', today) 

    # The name of the zip file 
    target = today + os.sep + now + '.zip' 

    # 5. We use the zip command to put the files in a zip archive 
    zip_command = "zip -qr {0} {1}".format(target, ' '.join(source)) 
    print(zip_command) 
    # Run the backup 
    if os.system(zip_command) == 0: 
     print('Successful backup to', target) 
    else: 
     print('Backup FAILED') 

これはエラーをプルアップ:

Traceback (most recent call last): 
     File "backup_ver2.py", line 23, in <module> 
     os.mkdir(today) # make directory 
    TypeError: mkdir: illegal type for path parameter 

私のソリューション:

今、私は私が困ってんだ知っているモジュールで

Traceback (most recent call last): 
     File "backup_ver2a.py", line 23, in <module> 
     os.makedirs(today, exist_ok=True) # make directory 
     File "/usr/lib/python3.4/os.py", line 222, in makedirs 
     head, tail = path.split(name) 
     File "/usr/lib/python3.4/posixpath.py", line 103, in split 
     i = p.rfind(sep) + 1 
    AttributeError: 'int' object has no attribute 'rfind' 

このトレースバックが参照している行:エラーが発生します

import os 
    import time 

    today = 14052016 # I set today as a string to solve a previous issue. 

    ..... 
    # Create the subdirectory if it isn't already there. 
    if not os.path.exists(today): 
     os.makedirs(today, exist_ok=True) # make directory 
     print('Successfully created directory', today) 

。変数「今日」がこれらの両方のエラーの中心にある可能性はありますか?非常に多くのエラーが発生しないように、またはサブディレクトリを確認して作成するためのよりよい方法があるように、今日を定義するためのより良い方法はありますか?あなたが彼の例でより多くの誤りに気付いたら、それらを訂正しないでください。私はすぐにそれらを見つけると確信しています。 :)ありがとうございました。

注:私は@gdlmxに同意のUbuntuに14.04 LTSと使用のpython 3

+1

'today = 14052016'これはあなたが「文字列」と言ったことと矛盾します。文字列を引用符で囲む必要があります – gdlmx

答えて

0

を実行している、両方のエラーは、「今日」あなたの変数から生じたint型ではなく文字列であり、したがって、あなたは簡単に必要があります次のコード行のように、引用符でそれを置くことによって文字列にint型からその変数に変更を加える:

today = "14052016" 

これは、あなたが消えていくべきである取得しているエラーを完了したら。

関連する問題