+ 6
How to open a file ?
I would like To open text file in order to Add lines and to use lines to calculate some operations , wich mode i should use ? W , r , a , w+.... ?
18 Antworten
+ 4
Abdelkader Barraj try
f=open("somefile.txt","r+") instead
https://www.pythonforbeginners.com/files/reading-and-writing-files-in-JUMP_LINK__&&__python__&&__JUMP_LINK
+ 5
Thank's guys for helping me ,
I'm very grateful
The answer is "r+" , it work
+ 4
f=open("somefile.txt", "a+") will allow you append
+ 3
a+ , and a , does not work for reading ,
+ 3
Mirielle👽 : i mean when i put "r" i cant modify or add , and when i put "a" i cant use lines to calcul something , i want modify and read in the 1 program
+ 3
Mirielle👽 excuse me for the langage , i cant explain correctly in english
+ 3
Shobha Gadula default is " r " , no it does not work
+ 3
If you open file, you need close it:
f = open("somefile.txt", "r")
f.close()
Use "with". It's best practice:
with open("example.txt") as f
f.write('Hello \n World')
Now file closed automatically.
+ 2
r+ is the mode you should use
+ 2
Phindile Pndy this discussion don't have a meaning. It's flood as everything in sololearn...
+ 1
File=open("filename.txt")
+ 1
Abdelkader Barraj check filename, path, os privileges (can you write file by notepad.exe?)
+ 1
Abdelkader Barraj copy this your code for analysis
+ 1
I really wanna learn this stuff
+ 1
open file as you open and use readlines() or read().split()
for mathematical operation you need to convert your string as int("your_string")
your program is like
file = open("executable file path", r)
content = file.read().split()
//for mathematical operation
for line in content:
line = int(line[0])
+ 1
What is the really meaning of this discussion group?