2016-08-24 8 views
0

ちょっと、ディレクトリ内のすべてのflvビデオファイルをmp4形式に変換するこのスクリプトがあります。forループ-pythonでos.chdirを使用してディレクトリに変更する際にエラーが発生しました

すべてのファイルが1つのディレクトリに置かれているだけで動作するようになっていますが、変更する必要があるため、そのディレクトリ内のフォルダに移動し、各フォルダ内のファイルを変換します。ここで

は私のコードは、私はPythonスクリプトに新しいです。これは、私が

sudo ./sub_start_comp.py 
Scanning directory: /home/dubit/Desktop/test/ 
convert file: 20051210-w50s.flv 
convert file: barsandtone.flv 
Traceback (most recent call last): 
File "./sub_start_comp.py", line 66, in <module> 
    main() 
File "./sub_start_comp.py", line 63, in main 
    flv_to_mp4.convert_flv_file_to_mp4_file() 
File "./sub_start_comp.py", line 46, in convert_flv_file_to_mp4_file 
    os.chdir(z) 
OSError: [Errno 2] No such file or directory: 'h' 

を受け付けておりますエラーです

sourcedirectory="/home/dubit/Desktop/test/" 

class ConvertToMP4: 
    def __init__(self): 
     self.flvfiles = [] 

    def fetch_flv_files(self, directory_name): 
     print("Scanning directory: " + directory_name) 
     for (dirpath, dirnames, filenames) in os.walk(directory_name): 
      for files in filenames: 
       if files.endswith('.flv'): 
        print('convert file: ' + files) 
        self.flvfiles.append(files) 

    def convert_flv_file_to_mp4_file(self): 
     # check to see if the list is empty, if not proceed 
     num_of_dir = len(list(os.walk(sourcedirectory))) 
     if len(self.flvfiles) <= 0: 
      print("No files to convert!") 
      return 
     for x in range(num_of_dir): 
      for (dirpath, dirnames, filenames) in os.walk(sourcedirectory): 
       for z in dirpath: 
        os.chdir(z) 
        for flvfiles in filenames: 
         mp4_file = flvfiles.replace('.flv','.mp4') 
         cmd_string = 'ffmpeg -i "' + flvfiles + '" -vcodec libx264 -crf 23 -acodec aac -strict experimental "' + mp4_file + '"' 
         print('converting ' + flvfiles + ' to ' + mp4_file) 
         os.system(cmd_string) 


def main(): 
    usage = "usage: %prog -d <source directory for flv files>" 
    parser = OptionParser(usage=usage) 
    parser.add_option("-d","--sourcedirectory",action="store", 
     type="string", dest="sourcedirectory", default="./", 
     help="source directory where all the flv files are stored") 
    (options, args) = parser.parse_args() 
    flv_to_mp4 = ConvertToMP4() 
    flv_to_mp4.fetch_flv_files(sourcedirectory) 
    flv_to_mp4.convert_flv_file_to_mp4_file() 
    return 0 

main() 

あるので、本当に任意のヘルプを問題をうまくするための知識を持っていません感謝されます。私がdirpath変数から最初の文字を取ることを推測しなければならない場合。

+0

'dirpath'は文字列です。あなたはなぜそれ以上のiteratigですか? –

答えて

4

dirpathは、単一文字列です。 forループ(for z in dirpath)を使用して反復処理すると、文字列の各文字を繰り返し処理しています'/home/dubit/Desktop/test/'!最初にz'/'に設定されている場合、'h' ...となります。ルートディレクトリにhという名前のディレクトリがないため、chdirが失敗します。


だけ

os.chdir(dirpath) 

for z in dirpath: 
    os.chdir(z) 

を交換し、それに応じてインデントを調整し、それが動作するはずです。

+0

ありがとうございます。将来のために知っておくと便利なことはすべて現在では –

1

エラーは、dirpathが実際には文字列であり、文字列のリストではないという事実のためです。 os.walk()はタプルを返します。dirpathは現在のディレクトリがdirnamesfilenamesdirpathの内容です。

文字列はPythonでiterableなので、インタープリタエラーはありませんが、代わりにdirpath文字列の各文字をループします。同様に、 for i, c in enumerate("abcd"): print('index: {i}, char: {c}'.format(i=i, c=c)) 意志出力 index: 0, char: a index: 1, char: b index: 2, char: c index: 3, char: d

だから、あなたべきchdir()dirpathに、それ以上ではないループ。ファイルシステムのルーピングはos.walk()によって内部的に行われます。

+0

知識のためにありがとう。 –

関連する問題