Question

I need a lot of questions for practicing writing and reading file in Python program to...

I need a lot of questions for practicing writing and reading file in Python program to study for my final, but I couldn't find it anywhere. Help me, please! :(

And also, I barely understand file in Python, so the questions need to be from easy and moderate, please!

Thanks for helping!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

when we want handle large amount of data we used files.

we store the data into files we perform operations on the file.

we take inputs in many ways in python one way is taking input from user by using input() method.

the another way take input from files.files are many types.

some are text files some are csv files csv files are each column separated with comma.

so based on the file type we can perform operation.

file is a object it contain many methods.

open()=This method is uses to open a file and which kind of mode we can pass as a second argument.

modes are very important when we dealing with file.

mode type->

r->when we use r mode we only do read the data from file.

w->when we use write mode we only able to write the data into file.we didn't able to read the data.

r+->when we used r+ mode we did able to read and write the data into file

w+->when we used w+ write and read the data from file

a->append the data into file.append is most important mode in files because when we open a file in write mode.the file contain some text.if we used write mode the file contain data override and replace with what we type.

so we lost important data from file.if we used "a" mode we can simply data append to file data.

the data didn't override.after the data our producing data will append to the file.

x->this mode much not import just if the file doesn't exist we can create a file using x mode.

read() method:

this method is used to read the data from file and gives entire file as a string.

readlines() method:

this method used to read the data from file and each line make a list object

write() method:

this method used to write the data into file.

we see one by one withh examples.

fiirst we deal with open and read:

open the file and read the data from file.

i have file namana.txt it contain iam HomeworkLib expert

here we used mode is "r" because we read the data from file.

we must remember one thing when we open a file after completion file operation we must close the file.

namana.txt

iam HomeworkLib expert

#output:

python source code file and text file must in same directory.if the file in other directory we must declare the full path of the file,

next write the data into file when we used write method automatically file created if the file doesnot exist on directory.

but when we read operation the file must be there.

write the data into file.

so we used write the data into file so we used write mode.

source code:

#output namana1.txt file is:

here we see why we used append mode:

namana1.txt contain data "iam vishnu vardhan"

so i want add his age.i used write method what will happend we see

source code:

#output:

"iam vishnu vardhan" replace with age=34 so this is override of data.that mean past file data erased and new data only update into file.so that's why based on condition we are going with append mode.it doesnpt override the data.

ex:

here we used append mode "a" and we append the data year=2019.

lets see the output.

source code:

#output:

based on the data output we can perform any operation..

#if you have any doubts comment below..if you like give thums up.....

Add a comment
Know the answer?
Add Answer to:
I need a lot of questions for practicing writing and reading file in Python program to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help writing a program in python that reads from a file and performs matrix...

    I need help writing a program in python that reads from a file and performs matrix multiplication. the file has this format 1 4 2 1 1 1 1 1 1 2 2 3 3 4 4 which means : 1 #rows in A 4 #cols in A, rows in B 2 #cols in B Matrix A contents: 1 1 1 1 Matrix B contents: 1 1 2 2 3 3 4 4 i need to be able to read...

  • Python question When reading and writing into a file. Say i want to read in input...

    Python question When reading and writing into a file. Say i want to read in input using a file called input.txt and then do something. Then i want to output the results to an additional file called output.txt. i have seen ouput.out or results.out used and not .txt. What is the difference? When would you use .out and not .txt? thank you

  • i need help with a c++ program, it will be reading characters in from a file...

    i need help with a c++ program, it will be reading characters in from a file using file stream while the program is reading chartacter by charecter , it will identify what is it reading so say a file contains this x = 10; (This is one line) y = 20;    (This is one line)                                                                                                                                                                                                                             print x +y; (This is one line)                                                                                                                                                                                                                    the outcome would be this x is a word 10 is a number y is a word...

  • I am writing a C program that takes either command line arguments or has a file...

    I am writing a C program that takes either command line arguments or has a file passed to it using < on the command line. My question is how do I check to see if there is a file passed using <. I think I need a way to check stdin to see if there is any data present. Thanks

  • Python Programming 4th Edition: Need help writing this program below. I get an error message that...

    Python Programming 4th Edition: Need help writing this program below. I get an error message that says that name is not defined when running the program below. My solution: def main(): filename = "text.txt" contents=f.read() upper_count=0 lower_count=0 digit_count=0 space_count=0 for i in contents: if i.isupper(): upper_count+=1 elif i.islower(): lower_count+=1 elif i.isdigit(): digit_count+=1 elif i.isspace(): space_count+=1 print("Upper case count in the file is",upper_count) print("Lower case count in the file is",lower_count) print("Digit count in the file is",digit_count) print("Space_count in the file is",space_count)...

  • After reading the questions carefully, please prove and compute the questions with clear hand writing. I...

    After reading the questions carefully, please prove and compute the questions with clear hand writing. I need to understand clearly, so when you prove these questions, please prove it step by step clearly!!!! 3- Let f: [0,1R be defined by f(x) = x2. For each n e N, let P be the partition of [0, 1 into n equal subintervals 3-1) Find formulas for U (f, P,) and L(f, P,). You may use the formula 2 = " n)without proof....

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • I need to write a C++ program where I need to read in a file and...

    I need to write a C++ program where I need to read in a file and do somethings with information inside the file. We can NOT use arrays. The picture below is what is in the file. My question is how do I read in the file and return the number of studies ( the 5 in the file ) so I can pass it through an other funciton. we need to use ofstream commands.

  • I need help in Python. This is a two step problem So I have the code...

    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...

  • Hi I need some help writing a security code using python mongodb restful api. I just...

    Hi I need some help writing a security code using python mongodb restful api. I just need help on how to provide a security login to user to enter their username and password or to have a block in using extra access once they have logined and they want more access to the databases they will be force to sign out of the server but I just do not know how to start on it can someone show me how...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT