【问题标题】:Python pass a file as argumentPython将文件作为参数传递
【发布时间】:2014-03-23 22:10:11
【问题描述】:

我一直在研究一个 Python 问题。我正在尝试使用 Echoprint API 来整理我的音乐。所以我正在编写一些代码来为我做这件事。

这就是 API 的工作原理:

  1. 将歌曲名称作为命令行参数。
  2. 给出适当的结果。

但我正在编写一个必须“在内部”执行此操作的脚本。如中所示,脚本应该获取文件并执行查找并将结果输出到终端。 (基本上 - 没有提供命令行参数)

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

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

这是将歌曲作为命令行参数的代码:

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

【问题讨论】:

    标签: python mp3


    【解决方案1】:

    从那里,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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2011-07-06
      • 2021-12-14
      • 2012-07-13
      • 2011-01-20
      • 2021-09-27
      相关资源
      最近更新 更多