2016-11-26 118 views
0

这里是一个程序来复制一个目录到一个新的路径,但不管程序的目的,我想知道如何简单退出时按下输入而不是投入。我想选择退出而不是输入输入

这是我的尝试使用sys.exit。在第一次提示时('要复制哪个目录?'),我只需按回车键(不输入任何数据即可提示),它仍然问我第二个问题(“我可以问哪里?”)
我想在第一次提示时按回车键退出程序。

print "\n" * 5 
print "\033[1m" + "Be Careful." 
print "\033[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 ('') 
+0

嗨普约尔。我刚刚尝试过,并得到完全相同的结果。它不会导致它退出,它只会转到下一个提示。谢谢 – peer

+1

你不检查并退出,直到第二个问题后,为什么这种行为会让你感到惊讶? – jonrsharpe

回答

0

Python的运行一行行,因此检查,看看是否变屁股是空的,直到后问第二个问题不会有你想要的结果。

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) 
+0

完美。完全意义。 – peer