我想选择退出而不是输入

I'd like to have the choice to exit instead of entering input

这是一个将一个目录复制到新路径的程序,但无论该程序的用途如何,我想知道如何在按回车而不是输入时简单地退出。

这是我使用 sys.exit 的尝试。当在第一个提示 ('Which dir to copy?') 时,我只需按回车键(不输入任何数据进行提示),它仍然会问我第二个问题('And to where may i ask??>>)
我想在第一次提示时按回车键后退出程序。

print "\n" * 5
print "3[1m" + "Be Careful."
print "3[0m"
print  "\n\tThis program will make changes to your directories.\n\tProceed with caution."
print "\n" * 5
print "\n" * 2
print "Press enter at any prompt to exit."
print "\n" * 5



from sys import exit

import shutil, os
os.chdir('/Users/User/')
butt = raw_input('Which dir you want copy??>> ')
whr = raw_input('And to where may i ask??>> ')
if butt == '' or whr == '':
    exit(0)
else:
    shutil.copytree(butt, whr)



import os
inputfolder = raw_input('What\'s the path bro???>>>> ')
for foldarName, subfolders, filnames in os.walk(inputfolder):
    print('The current folder is ' + foldarName)

    for sub in subfolders:
        print('SUBFOLDER OF ' + foldarName + ': ' + sub)
    for filna in filnames:
        print('FILE INSIDE ' + foldarName + ': ' + filna)

    print ('')

Python 逐行运行,因此检查变量 butt 是否为空,直到 after 询问第二个问题不会有您想要的结果。

butt = raw_input('Which dir you want copy??>> ')
if butt == ''
    exit(0)

whr = raw_input('And to where may i ask??>> ')
if whr == '':
    exit(0)

shutil.copytree(butt, whr) # doesn't require an else statement

此外,更有效的方法是

if not butt:
    exit(0)