How to open a file ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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+.... ?

13th Feb 2020, 8:54 PM
Abdelkader Barraj عبد القادر البراج
Abdelkader Barraj عبد القادر البراج - avatar
16 Answers
+ 5
Thank's guys for helping me , I'm very grateful The answer is "r+" , it work
17th Feb 2020, 6:51 AM
Abdelkader Barraj عبد القادر البراج
Abdelkader Barraj عبد القادر البراج - avatar
+ 4
f=open("somefile.txt", "a+") will allow you append
13th Feb 2020, 9:08 PM
BroFar
BroFar - avatar
+ 3
a+ , and a , does not work for reading ,
13th Feb 2020, 9:46 PM
Abdelkader Barraj عبد القادر البراج
Abdelkader Barraj عبد القادر البراج - avatar
+ 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
13th Feb 2020, 11:31 PM
Abdelkader Barraj عبد القادر البراج
Abdelkader Barraj عبد القادر البراج - avatar
+ 3
Mirielle👽 excuse me for the langage , i cant explain correctly in english
13th Feb 2020, 11:32 PM
Abdelkader Barraj عبد القادر البراج
Abdelkader Barraj عبد القادر البراج - avatar
+ 3
Shobha Gadula default is " r " , no it does not work
14th Feb 2020, 9:54 AM
Abdelkader Barraj عبد القادر البراج
Abdelkader Barraj عبد القادر البراج - avatar
+ 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.
15th Feb 2020, 6:20 AM
Valerii Mamontov
Valerii Mamontov - avatar
+ 2
r+ is the mode you should use
15th Feb 2020, 7:20 PM
Aditya Singh Chauhan
Aditya Singh Chauhan - avatar
+ 2
Phindile Pndy this discussion don't have a meaning. It's flood as everything in sololearn...
15th Feb 2020, 8:45 PM
Valerii Mamontov
Valerii Mamontov - avatar
+ 1
File=open("filename.txt")
14th Feb 2020, 2:10 AM
Shobha Gadula
Shobha Gadula - avatar
+ 1
Abdelkader Barraj check filename, path, os privileges (can you write file by notepad.exe?)
15th Feb 2020, 8:38 AM
Valerii Mamontov
Valerii Mamontov - avatar
+ 1
Abdelkader Barraj copy this your code for analysis
15th Feb 2020, 8:42 AM
Valerii Mamontov
Valerii Mamontov - avatar
+ 1
I really wanna learn this stuff
15th Feb 2020, 9:27 AM
Abdulazeez BabaTunde
Abdulazeez BabaTunde - avatar
+ 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])
15th Feb 2020, 5:09 PM
Aritra Pramanik
Aritra Pramanik - avatar
+ 1
What is the really meaning of this discussion group?
15th Feb 2020, 8:28 PM
Phindile Pndy