我的代码的CSV部分做错了什么



因此,在我的python代码中,我应该提示用户输入他们的姓名、地址和电话号码,并使用目录和文件名将数据作为一行逗号分隔的值写入文件。除CSV部分外,所有内容都在运行。

import os
import csv
def file_system():
"""Display information about users"""
direc = input("Enter name directory to save a file: ")
filename = input("Enter name of the file they want to save to the directory: ") 
name = input("Enter your name : ")
address = input("Enter your address : ")
phone_number = input("Enter your phone number : ")
print  (direc, filename, name, address, phone_number)
prompt = input()

if os.path.isdir(direc):
writeFile = open(os.path.join(direc,filename),'w') 
writeFile.write (direc, + filename)
writeFile.close() 

print("File contents:")

readFile = open(os.path.join(direc,filename),'r') 
for line in readFile:
print(line)
readFile.close()
if prompt:
with open('Userdata.csv', 'a',
newline='') as outfile:

w = csv.writer(outfile)
w.writerow(name, address, phone_number)

print("File Updated")


else:
print("Directory doesn't exist, please enter again")
file_system()

问题在于csv部分之前的if语句的评估。

运行下面的代码两次,第一次只需按enter,第二次键入内容,然后按enter。。。

prompt = input("input something:n")
if prompt:
print("you got here")
else:
print("you didn't")

正如您所看到的,当只按下回车键时,if语句将变为False。。。

相关内容

  • 没有找到相关文章