2010-01-03 18 views
3
import os 
import pprint 
import subprocess 
def Convert (dir): 
    curDir = dir 
    pathToBonk = "C:\\Program Files\\BonkEnc\\becmd.exe" #Where the becmd.exe file lives 
    problemFiles = [] #A list of files that failed conversion 
    # 
    for item in os.listdir(curDir): 
     if item.upper().endswith('.M4A'): 
      fullPath = os.path.join(curDir,item) 
      cmd = '"%s" -e LAME -d "%s" "%s"' #The command to convert a single file 
      cmd = cmd % (pathToBonk, curDir, fullPath) 
      val = subprocess.call(cmd) 
      if val == 0: #Successfull conversion, delete the original 
       os.remove(fullPath) 
      else: 
       problemFiles.append(fullPath) 
       print 'Problem converting %s' % item 
       os.rename(fullPath, fullPath + ".BAD") 
    print 'These files had problems converting and have been renamed with .BAD extensions:' 
    pprint.pprint(problemFiles)  

var = raw_input("Insert Path: ") 
var.decode("iso-8859-8") 
Convert(var) 

您好, 我要重新格式化从.M4A我的音乐转换成MP3歌曲。 我使用bonkenc命令行。蟒蛇希伯来文输入 filesytem格式

问题是我的一些文件夹是用希伯来语写的。 当我在不包含希伯来语的文件夹中使用此脚本时 - 它的工作原理完美无瑕。 但是当路径中有希伯来语时,scrpit不起作用。

我试着编码\ deconding希伯来文,但没有任何帮助。

我运行windows xps p2。 在此先感谢, Liron。

+1

发现,如果你说的是什么发生问题,这将有助于。我知道你会得到一个例外 - 哪一个?哪里?是否与您运行的外部exe相关的问题,或者如果您只留下python代码的框架,是否也会发生这种问题? – daphshez 2010-01-03 06:59:23

+0

请参阅:http://stackoverflow.com/questions/497233/pythons-os-path-choking-on-hebrew-filenames – Oren 2010-01-03 00:18:50

+0

是的。但它有时会抛出我无法捕捉的异常。 – 2010-01-03 00:25:17

回答

0

只是使用os.listdir(unicode(str))而不是os.listdir(str)为了确保str是Unicode,否则它会失败。

同样的问题可以在this question