根据用户输入将Python保存到特定目录
我试图根据用户输入将图像保存到一个目录中。例如:根据用户输入将Python保存到特定目录,python,input,Python,Input,我试图根据用户输入将图像保存到一个目录中。例如: if user enters 'A' save in A folder elif user enters 'B' save in B folder 等等 当我尝试这两件事时,一件是文件夹没有填满,另一件是我的循环崩溃。我已经尝试使用getch()和input()有一段时间了,但这两种方法都不适合我 这是我的密码 getInput = input("Enter Which Window to Save") if getInput
if user enters 'A'
save in A folder
elif user enters 'B'
save in B folder
等等
当我尝试这两件事时,一件是文件夹没有填满,另一件是我的循环崩溃。我已经尝试使用getch()和input()有一段时间了,但这两种方法都不适合我
这是我的密码
getInput = input("Enter Which Window to Save")
if getInput == int('1'):
cardFound = input("Which Card was Found: ")
cardsFound.append(cardFound)
print("\tFlop Cards Found")
print(cardsfound)
print (52 - counter1,"Left to find...")
cv2.imwrite("C:/FlopOne/" + cardFound + ".jpg")
cv2.waitKey(0)
在这之后,有很多elif语句都响应getInput,但是当循环为getInput暂停时。我的窗户(一共有五扇)没有打开,只有灰色的屏幕。然而,如果我调用waitKey()来查看我的窗口,那么循环将拖拉,我将无法获得输入。我不想手动解析此文件夹
注意,我现在才学习Python。在处理路径和目录时,应该使用os.path模块。(这不是必需的,但它使处理路径变得更容易)。这个模块使跨平台代码变得更容易一些,这些代码将在windows和linux上运行,即使目录和路径约定看起来不同。下面是一个选择目录并写入目录的小例子 这个例子有一个while循环,只要输入不是“e”,它就会不断地请求输入。用户可以写入目录a或目录b。从这里,我们将使用os.path.join()附加目录和随机文件名。请注意,我没有使用unix样式的路径或windows样式的路径。如果您想在本地运行这个程序,只需确保创建目录“a”和目录“b”
import os
from random import randint
if __name__ == '__main__':
# This is the current working directory...
base_path = os.getcwd()
while True:
# whitelist of directories...
dirs = ["a", "b"]
# Asking the user for the directory...
raw_input = input("Enter directory (a, b): ")
# Checking to be sure that the directory they entered is valid...
if raw_input in dirs:
# Generating a random filename that we will create and write to...
file_name = "{0}.txt".format(randint(0, 1000000))
# Here we are joining the base_path with the user-entered
# directory and the randomly generated filename...
new_file_path = os.path.join(base_path, raw_input, file_name)
print("Writing to: {0}".format(new_file_path))
# Writing to that randomly generated file_name/path...
with open(new_file_path, 'w+') as out_file:
out_file.write("Cool!")
elif raw_input == 'e':
break