Python将文件作为参数传递

时间:2014-02-22 06:24:38

标签: python mp3

我一直在研究Python问题。我正在尝试使用Echoprint API对我的音乐进行排序。所以我正在编写一些代码来为我做这些。

这就是API的工作原理:

  1. 将歌曲名称作为命令行arg。
  2. 给出适当的结果。
  3. 但我正在编写一个必须在内部执行此操作的脚本。如同,脚本应该获取文件并执行查找并将结果输出到终端。 (基本上 - 没有提供命令行论点)

    那么无论如何将文件传递给函数? 我知道这听起来很傻,但这是一个我无法解决的问题。

    如果我使用os.walk()等,它会将str对象作为参数返回到查找函数。我希望音频文件作为参数传递。

    这是将歌曲作为命令行arg:

    接收的代码
    import sys
    import os
    
    import pyechonest.config as config
    import pyechonest.song as song
    
    config.CODEGEN_BINARY_OVERRIDE = os.path.abspath("/Users/******/python/minger/echoprint-codegen-master/echoprint-codegen")
    config.ECHO_NEST_API_KEY='*****'
    
    def lookup(file):
    # Note that song.identify reads just the first 30 seconds of the file
        fp = song.util.codegen(file)
        if len(fp) and "code" in fp[0]:
            # The version parameter to song/identify indicates the use of echoprint
            result = song.identify(query_obj=fp, version="4.11")
            print "Got result:", result
            print result[0]
            if len(result):
                print "Artist: %s (%s)" % (result[0].artist_name, result[0].artist_id)
                print "Song: %s (%s)" % (result[0].title, result[0].id)
            else:
                print "No match. This track may not be in the database yet."
        else:
            print "Couldn't decode", file
    
    
    if __name__ == "__main__":
        if len(sys.argv) < 2:
            print >>sys.stderr, "Usage: %s <audio file>" % sys.argv[0]
            sys.exit(1)
        lookup(sys.argv[1])
    

1 个答案:

答案 0 :(得分:0)

从那里http://echonest.github.io/remix/apidocs/pyechonest.util-module.html#codegen

您使用的方法有签名

codegen(filename, start=0, duration=30)

所以它是必须作为参数传递的文件名...而不是文件本身......

在此使用http://nullege.com/codes/show/src@p@y@pyechonest-7.1.0@pyechonest@song.py/371/util.codegen

if filename:
        if os.path.exists(filename):
            query_obj = util.codegen(filename, start=codegen_start, duration=codegen_duration)
            if query_obj is None:
                raise Exception("The filename specified: %s could not be decoded." % filename)