这是一个将一个目录复制到新路径的程序,但无论程序的目的如何,我都想知道如何在按Enter而不是输入时退出。
以下是我使用sys.exit
的尝试。在第一次提示时('哪个目录要复制?'),我只需按回车(输入无数据提示),它仍然会问我第二个问题('以及到哪里我可以问?>>)
我想在第一个提示符下按Enter键后退出程序。
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 :(得分:0)
Python逐行运行,因此检查变量 butt 是否为空,直到之后询问第二个问题不会得到您想要的结果。
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)