2016-08-24 113 views
0

嘿,我已經有了這個腳本,它將目錄中的所有flv視頻文件轉換爲mp4格式。在for循環中使用os.chdir更改目錄時出錯 - python

我只是將所有文件放在一個目錄中,但現在需要對其進行修改,以便它進入該目錄中的文件夾並轉換每個文件夾中的文件。

這裏是我的代碼

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() 

這是我收到的錯誤

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' 

我是新來的Python腳本等等都沒有真正得到了知識,鍛鍊了問題,所以任何幫助將不勝感激。如果我不得不猜測它只是從dirpath變量中取出第一個字符。

+0

'dirpath'是一個字符串。你爲什麼重複它? –

回答

4

dirpath是一個字符串。當您使用for循環(for z in dirpath)遍歷它時,您正在遍歷字符串'/home/dubit/Desktop/test/'中的每個單獨字符!首先z設置爲'/',然後'h' ...這就是chdir失敗的原因,因爲根目錄中沒有名爲h的目錄。


只是

os.chdir(dirpath) 

更換

for z in dirpath: 
    os.chdir(z) 

,並相應調整縮進,它應該工作。

+0

謝謝非常。有用知道未來和它的所有現在相應地工作 –

1

你得到的錯誤是由於dirpath實際上是一個字符串,而不是字符串列表。 os.walk()返回一個元組,其中dirpath是當前目錄正在走過,dirnamesfilenamesdirpath的內容。

由於字符串在Python中是可迭代的,因此不會發生任何解釋器錯誤,而是循環遍歷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

Thankyou的知識。 –