首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python将文件作为参数传递

Python将文件作为参数传递
EN

Stack Overflow用户
提问于 2014-02-22 06:24:38
回答 1查看 5.4K关注 0票数 1

我研究Python问题已经有一段时间了。我正在尝试使用Echoprint来整理我的音乐。所以我在为我写一些代码。

API是这样工作的:

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

但我正在写一个脚本,必须在“内部”执行这一任务。与前面一样,脚本应该获取文件并执行查找,并将结果输出到终端。(基本上-没有提供命令行参数)

那么,是否存在将文件传递到函数中的问题?我知道这听起来很傻,但这是一个我无法解决的问题。

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

下面是将歌曲作为命令行arg的代码:

代码语言:javascript
复制
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])
EN

回答 1

Stack Overflow用户

发布于 2014-02-22 08:14:34

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

您使用的方法有签名。

代码语言:javascript
复制
codegen(filename, start=0, duration=30)

所以必须将文件名作为参数传递.不是文件本身..。

以前在这里使用过http://nullege.com/codes/show/src@p@y@pyechonest-7.1.0@pyechonest@song.py/371/util.codegen

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21950628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档