I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions:
and here is my code:
update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated.
There were some mistakes in your code. Mainly, you were not closing the files once a write operation is performed. One thing to keep in mind is that whenever you open a file, you should close it. Here is the corrected code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
def writeToFile(string,filename):
with open(filename,'w') as x:
x.write(string)
#closing file
x.close()
def printFile(filename):
#opening file
file=open(filename)
#printing complete file after truncating trailing/leading newline characters
print(file.read().strip())
file.close() #closing file
def writeListToFile(l,filename):
#opening file in write mode
file=open(filename,'w')
#looping and writing each number to file, followed by a newline character
for num in l:
file.write(str(num)+'\n')
file.close() #closing file to save everything
def writeUserInfo(name,password,filename):
#opening file in append mode
file=open(filename,'a+')
#appending name and password in the mentioned format
file.write('name='+name+'\n')
file.write('password='+password+'\n')
file.close()#closing file to save everything
def sumFile(filename):
sum=0
file=open(filename)
#looping through each line
for line in file:
#converting line to int
num=int(line.strip())
#adding to sum
sum+=num
#closing file and returning sum
file.close()
return sum
def nameInFile(name, filename):
file=open(filename)
for line in file:
#stirpping newline character from line and checking if line equals name=<name>
if line.strip()=='name='+name:
return True
file.close()
return False
def main():
writeToFile("Hello, World\n","hello.txt")
printFile("hello.txt")
writeListToFile([1,2,3,4],"myfile.txt")
printFile("myfile.txt")
writeUserInfo("paul","walrus7","secret_file")
writeUserInfo("dave", "python_is_fun", "secret_file")
printFile("secret_file")
print(sumFile("myfile.txt"))
print(nameInFile('paul','secret_file'))
main()
#output
Hello, World
1
2
3
4
name=paul
password=walrus7
name=dave
password=python_is_fun
10
True
I am having a little trouble with my Python3 code today, I am not sure what...
I am having trouble trying to output my file Lab13.txt. It will say that everything is correct but won't output what is in the file. Please Help Write a program that will input data from the file Lab13.txt(downloadable file); a name, telephone number, and email address. Store the data in a simple local array to the main module, then sort the array by the names. You should have several functions that pass data by reference. Hint: make your array large...
I need help in Python. This is a two step problem So I have the code down, but I am missing some requirements that I am stuck on. Also, I need help verifying the problem is correct.:) 7. Random Number File Writer Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500. The application should let the user specify how many random numbers the file...
I am new to Python and am having trouble coming up with writing code to the following problem... The program must: Prompt for a file name Opens that file and reads through the file Displays a custom error message if the file does not exist You can pull the hour out from the 'From ' line by finding the time and then splitting the string a second time using a colon. From [email protected] Sat Jan 5 09:14:16 2008 Accumulated the...
I am having problems with reading a file into an array. This is my code. This is what I get when I run my program. But this is my text file I am reading. I tried everything and it seems to be reading in the last digit of the file. I want to read in their names line by line into an array and ultimatly also read in the scores line by line. 1 E/7 Programming Assignment 6.cpp Defines the...
Hey all, I am writng some code nad this is what I have so far, I am stuck -Write a function named "write_list" that takes a list of strings as a parameter and writes the contents of the list to a file named "stranger.txt" with one value per line. If a file named "stranger.txt" already exists it must be overwritten. The function should not return any value def write_list(lst): with open ("stranger.txt","w") as file: for string in lst: file.write(string+'\n') write_list(lst)
Python3, write a program, specific information is in the graphs, I got 7 marks out of 10, you may change my codes to let it gives correct output,restrications are also on the graph, for loop, in key word, enumerate,zip,slices, with key word can't be used. All the information is on the graph, is_shakespeare_play(line) may be a function given that can check whether is Shakespeare play Question 2- 10 marks Write the function keep titles short (filename, max charactars) which takes...
Lab 3: Databased Admin Tool Python Objective: Develop an admin tool for username and password management. You will need to use the best data structure to store the data you read from UD.txt for efficient processing. You can accomplish these tasks with Dictionary or List. The data file (UD.txt) : FIRST NAME, LAST NAME,USERNAME,PASSWORD Sam, DDal,sdd233,Pad231 Dave,Dcon,dcf987, BHYW4fw Dell,Grant,dgr803,Sb83d2d Mike,Kress,mkr212,UNNHS322 Lisa,Kate,lki065,dgw6234 Paul,Edward,ped332,9891ds Youyou,Tranten,ytr876,dsid21kk Nomi,Mhanken,nmh223,3282jd3d2 Write a program that imports the database from UD.txt Give the following options: A: Search by...
# DISCUSSION SECTION WORK: # # 1. STUDENTS: download this file, ds4.py, and wordsMany.txt, from # http://www.cs.uiowa.edu/~cremer/courses/cs1210/etc/ds4/ # Save both in the same folder. # # 2. TA (aloud) and STUDENTS: Read the comments from START HERE! (just after these instructions) # to definition of anagramInfo function. Discuss any questions about what the functions should do. # # 3. TA demonstrate running anagramInfo("wordsMany.txt") on this unchanged file, to # see that it behaves reasonably despite having incomplete anagram-testing functions. #...
I am having a hard time with my program to assignment 4.12. Here are the instructions: The Payroll Department keeps a list of employee information for each pay period in a text file. The format of each line of the file is the following: <last name> <hours worked> <hourly wage> Write a program that inputs a filename from the user and prints to the terminal a report of the wages paid to the employees for the given period. The report...
C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...