import os.path
import re
def request():
print ("What file should I write to?")
file = input()
thing = os.path.exists (file)
if thing == True:
start = 0
elif re.match ("^.+.\txt$", file):
stuff = open (file, "w")
stuff.write ("Some text.")
stuff.close()
start = 0
else:
start = 1
go = "yes"
list1 = (start, file, go)
return list1
start = 1
while start == 1:
list1 = request()
(start, file, go) = list1
拿起每當我進入Thing.txt
作爲文本,該elif
應該抓住,它在給定的格式。但是,start
不會更改爲0
,並且不會創建文件。我是否錯誤地格式化了re.match
?re.match不上的txt文件格式
'if file.endswith('txt')'解決你的正則表達式問題。 –