How to open a file ? | SoloLearn: Learn to code for FREE!

+1

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 Answers

New Answer

+2

Abdelkader Barraj try f=open("somefile.txt","r+") instead https://www.pythonforbeginners.com/files/reading-and-writing-files-in-python

+3

f=open("somefile.txt", "a+") will allow you append

+3

Thank's guys for helping me , I'm very grateful The answer is "r+" , it work

+2

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.

+1

Append (a)

+1

a+ , and a , does not work for reading ,

+1

Abdelkader Barraj what do you mean by "does not work for reading? Must be writing the wrong code

+1

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

+1

Mirielle👽 excuse me for the langage , i cant explain correctly in english

+1

Shobha Gadula default is " r " , no it does not work

+1

r+ is the mode you should use

+1

Phindile Pndy this discussion don't have a meaning. It's flood as everything in sololearn...

0

File=open("filename.txt")

0

Abdelkader Barraj check filename, path, os privileges (can you write file by notepad.exe?)

0

Abdelkader Barraj copy this your code for analysis

0

I really wanna learn this stuff

0

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])

0

What is the really meaning of this discussion group?