Python Forum
Can we store value in file if we open file in read mode?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can we store value in file if we open file in read mode?
#1
Using the open() function, open the test.txt file in reading mode and store it in the variable called file_2

Is this possible to create variable when we open a file in read mode?
Reply
#2
Yes you can always create variables.
Reply
#3
(Sep-26-2020, 07:51 AM)ibreeden Wrote: Yes you can always create variables.

Hi Could you please explain the syntax how to do it ? as I tried with below syntax but no successes
file = open('test.txt', 'r')
file.write('file_2')
Reply
#4
Please read 7.2. Reading and Writing Files and open(). You opened the file for reading, so you cannot write to it. If you want to write to a file you must open it for writing ("w") or appending ("a") or read-write ("+").
file = open('test.txt', 'w')
file.write('file_2')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Open a setup python file? Adlo777 2 1,890 Jan-28-2021, 08:10 PM
Last Post: nilamo
  [split] how to read a specific row in CSV file ? laxmipython 2 8,853 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,965 May-15-2020, 04:57 PM
Last Post: snippsat
  Read text file, process data and print specific output Happythankyoumoreplease 3 2,902 Feb-20-2020, 12:19 PM
Last Post: jefsummers
  open file from listbox to Text elstolbo 0 2,358 Jan-21-2020, 09:37 PM
Last Post: elstolbo
  help! function that checks if a file is a bed file, tips so i can code it myself lilyS 1 2,359 May-31-2019, 12:41 PM
Last Post: ichabod801
  read from file mcgrim 16 6,093 May-14-2019, 10:31 AM
Last Post: mcgrim
  read words from the keyboard and store them geoe 8 4,445 Jan-13-2019, 08:36 PM
Last Post: Larz60+
  reading text file and writing to an output file precedded by line numbers kannan 7 10,366 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Read directly from excel file using python script dvldgs05 0 2,250 Oct-19-2018, 02:51 AM
Last Post: dvldgs05

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020