2016-08-24 49 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的知识。 –