-2

我有用于创建 sqlite 数据库的当前代码:

 import storage
 import os
 import audiotools

 def store_dir(d):
     store = storage.HashStore()
     for root, bar, files in os.walk(d):
         for filename in files:
             filename = root + '/' + filename

             try:
                 store.store_file(filename)
                 print ("Stored %s% filename")
             except audiotools.UnsupportedFile:
                 print ('Skipping unsupported file %s') % filename
             except Exception, e:
                 print (e)

 def main():
     d = input('Enter the path to the music directory: ')
     store_dir(d)
     print ("Done.")

 if __name__ == '__main__':
     main()

当此代码运行时,我收到语法错误消息。请帮忙 !提前致谢

4

1 回答 1

0

这里有几件事要解决。

首先,这一行:

print ('Skipping unsupported file %s') % filename

需要是这样的:

print ('Skipping unsupported file %s' % filename)

其次,你需要在raw_input这里使用:

d = input('Enter the path to the music directory: ')

它返回一个字符串对象,而不是input,它将输入评估为真正的 Python 代码。

第三,您的缩进已关闭。我很确定这只是一个 SO 格式错误。

最后,你应该os.path.join在这里使用:

filename = root + '/' + filename

但这不是错误,只是提示。

总而言之,您的代码应如下所示:

import storage
import os
import audiotools

def store_dir(d):
    store = storage.HashStore()
    for root, bar, files in os.walk(d):
        for filename in files:
            filename = os.path.join(root, filename)

            try:
                store.store_file(filename)
                print ("Stored %s% filename")
            except audiotools.UnsupportedFile:
                print ('Skipping unsupported file %s' % filename)
            except Exception, e:
                print (e)

def main():
    d = raw_input('Enter the path to the music directory: ')
    store_dir(d)
    print ("Done.")

if __name__ == '__main__':
    main()
于 2013-10-27T17:44:49.710 回答