如何将这两个Python脚本合并?

huangapple go评论76阅读模式
英文:

how can i add this two python scripts together?

问题

# 这是我的Python脚本,看起来像这样:

New_folder = "C:\Songs"

arr_txt = [x for x in os.listdir(New_folder) if x.endswith(".mp3")]

testfile = [New_folder]

for i in range(len(testfile)):

for x in testfile:

    print(*arr_txt, sep="\n")

    print()

# 我想要添加的第二部分是一个字母转换器,其脚本如下:

from unidecode import unidecode
s = ""  # 这里的脚本 <---
s = unidecode(s)
print(s)

# 所以我希望第二部分与第一个脚本连接起来,第一个脚本的任何结果都传递到第二个脚本,以便进行转换
英文:

so i have this python script which looks like this:

New_folder = &quot;C:\Songs&quot;

arr_txt = [x for x in os.listdir(New_folder) if x.endswith(&quot;.mp3&quot;)]

testfile = [New_folder]

for i in range(len(testfile)):

for x in testfile:

    print(*arr_txt, sep=&quot;\n&quot;)

    print()

which reads all the names of the files in my directory.

and the second part i want to add is a letter converter which it's script looks like:

from unidecode import unidecode
s = &quot;&quot;  #the script in there &lt;---
s = unidecode(s)
print(s)

so I want the second part to connect with the first script and whatever result is coming out of the first script to go into the second script so that it gets converted

答案1

得分: 1

你可以在Python中使用函数。

在一个文件中,让我们称之为 extract.py,你应该指定其他脚本要使用的预定义方法,例如:

# extract.py

def get_file_names():
  New_folder = "C:\Songs"
  arr_txt = [x for x in os.listdir(New_folder) if x.endswith(".mp3")]
  return arr_txt # 这对调用者来处理提取的值非常重要

然后在另一个文件中,比如说 process.py,你可以从 extract.py 中调用你想要使用的方法。这可以通过 import 来实现:

# process.py
from extract import get_file_names
from unidecode import unidecode

s = get_file_names()
for file_name in s:
  print(unidecode(file_name))

这个解决方案应该适用于使用最新的Python解释器。请注意,在Python 2.x 中,import 的工作方式略有不同。

英文:

You can utilise functions in Python.

In one file, let's call this extract.py, you should specify the pre-defined method for other scripts to use, e.g.

# extract.py

def get_file_names():
  New_folder = &quot;C:\Songs&quot;
  arr_txt = [x for x in os.listdir(New_folder) if x.endswith(&quot;.mp3&quot;)]
  return arr_txt # this is important for caller to process the extracted value

And then in another file, let's say process.py, you call the method you want to use from extract.py. This can be done using import

# process.py
from extract import get_file_names
from unidecode import unidecode

s = get_file_names()
for file_name in s:
  print(unidecode(filename))

This solution should work for using the latest Python interpreter. Please do note that import works slightly different in Python 2.x.

答案2

得分: 0

以下是翻译好的代码部分:

from unidecode import unidecode
def playmusic():
    New_folder = "C:\Songs"
    arr_txt = [x for x in os.listdir(New_folder) if x.endswith(".mp3")]
    testfile = [New_folder]
    for i in range(len(testfile)):
        for x in testfile:
            print(*arr_txt, sep="\n")
            print()
playmusic()
s = ""
s = unidecode(s)
print(s)
英文:

Something like below

from unidecode import unidecode
def playmusic():
    New_folder = &quot;C:\Songs&quot;
    arr_txt = [x for x in os.listdir(New_folder) if x.endswith(&quot;.mp3&quot;)]
    testfile = [New_folder]
    for i in range(len(testfile)):
        for x in testfile:
            print(*arr_txt, sep=&quot;\n&quot;)
            print()
playmusic()
s = &quot;&quot;
s = unidecode(s)
print(s)

huangapple
  • 本文由 发表于 2020年1月6日 17:58:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定