Question
Hello I am trying to take the commands that i have done interactively, and create a python program that will create a file, collect data
Hello I am trying to take the commands that i have done interactively, and create a python program that will create a file, collect data from the user, and write the information to the file. I have hit a roadblock when it comes to creating the program in python however below is what I have done interactively in shell:
>>> import os
>>> os.getcwd()
'C:\\Users\\laure\\AppData\\Local\\Programs\\Python\\Python39'
>>> os.chdir("c:\\users\\laure")
>>> os.getcwd()
'c:\\users\\laure'
>>> if not os.path.exists("work"):
os.mkdir("work")
>>> os.chdir("work")
>>> os.getcwd()
'c:\\users\\laure\\work'
>>> account=open("accounts.txt","w+")
>>> os.listdir()
['accounts.txt']
>>> accountList=[]
>>> for i in range(5):
accountList.append(input("Please enter your name"))
Please enter your name Graham Chapman
Please enter your name John Cleese
Please enter your name Eric Idle
Please enter your name Michael Palin
Please enter your name Terry Giliam
>>> print(accountList)
[' Graham Chapman', ' John Cleese', ' Eric Idle', ' Michael Palin', ' Terry Giliam']
>>> for name in accountList:
account.write(name + " ")
16
13
11
15
14
>>> account.close()
>>> os.listdir()
['accounts.txt']
>>> filecontent = open("accounts.txt","r")
>>> for line in filecontent:
print(line)
Graham Chapman
John Cleese
Eric Idle
Michael Palin
Terry Giliam
>>>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started