脚本要求一个文件路径,但给出了b

2024-04-24 03:42:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Python脚本,它要求我选择输入文件夹。我正在OSX上运行它。你知道吗

问题是,当我尝试写入输入路径时,它会返回一个错误。假设输入文件夹是“Documents”,它的意思是:

MBPAdmin:moviep3 Admin$ python merge_videos.py
Enter path to input folder: /Documents
Traceback (most recent call last):
  File "merge_videos.py", line 13, in <module>
    input_folder = input(input_folder_label)
  File "<string>", line 1
    /Documents
    ^
SyntaxError: invalid syntax

需要帮忙吗?你知道吗

编辑: 这是部分代码,如果有用的话。你知道吗

from moviepy.editor import VideoFileClip, concatenate_videoclips

import time
import os
import sys


input_folder_label = 'Enter path to input folder: '
output_folder_label = 'Enter path to output folder: '
video_to_be_merged_label = 'Enter path to video that should be merged at the end of each video: '
default_output_folder_name = 'merged_videos' + str(time.time())

input_folder = input(input_folder_label)
input_folder = '/Users/burlicconi/Downloads/filmovi'
try:
    assert os.path.exists(input_folder)
except AssertionError as exc:
    print("Input folder was not found at {}".format(input_folder))
    sys.exit()
print('Input folder exists: {}'.format(input_folder))

Tags: topathimport文件夹inputoutputtimevideo
1条回答
网友
1楼 · 发布于 2024-04-24 03:42:36

问题是python2.x在使用^{}时会尝试评估用户输入,您需要在python2.x中使用^{},因此它不会尝试这样做。更改:

input_folder = input(input_folder_label)

input_folder = raw_input(input_folder_label)

要使它在Python2.x中工作,或者更好地隐藏Python2.x中的内置input(),因为您无论如何都不会这样使用它:

# after your imports, at the beginning of your script:
try:
   input = raw_input
except NameError:
   pass  # Python 3.x, ignore...

现在您可以在Python2.x和Python3.x上使用它

相关问题 更多 >