+ 2

open files in python

Hello everyone, I am learning the python core courses, and in exceptions and files, I don't understand, do somebody can help me. (I am french, if a french see this) thanks

22nd Nov 2021, 7:07 AM
Mathéo
Mathéo - avatar
18 Réponses
+ 11
create empty file : f = open("myfile.txt", "a") And also Create a new file if it does not exist: f = open("myfile.txt", "w")
22nd Nov 2021, 6:24 PM
Vaibhav
Vaibhav - avatar
+ 5
You can create empty file : f = open("myfile.txt", "x") And also Create a new file if it does not exist: f = open("myfile.txt", "w")
22nd Nov 2021, 7:40 AM
Zura Papiashvili
Zura Papiashvili - avatar
+ 5
for those of you, who are interested in reading some more informations about the file open modes: https://www.delftstack.com/howto/JUMP_LINK__&&__python__&&__JUMP_LINK/python-open-modes/
22nd Nov 2021, 10:45 AM
Lothar
Lothar - avatar
+ 3
The Most Reliable Way To Open Files In Python File Handling Methods: with open('filename.txt', 'w+') as read_write: # Your Code Here... read_write.close()
22nd Nov 2021, 8:11 AM
Sancho Godinho
Sancho Godinho - avatar
+ 3
АлКа Релова You Can Run This Keyword For Help With File Handling... >>help(open)
23rd Nov 2021, 11:50 AM
Sancho Godinho
Sancho Godinho - avatar
+ 3
АлКа Релова also look at "with" statement to work with files
23rd Nov 2021, 5:42 PM
Ilyas Bakirov
Ilyas Bakirov - avatar
+ 2
You can use try-except-finally block: try: file = open(filePath, 'w') file.write("Hello World!") except IOError: print("Unable to create file on disk.") finally: file.close()
22nd Nov 2021, 7:33 AM
Zura Papiashvili
Zura Papiashvili - avatar
+ 2
Can you send your task? I might suggest something then
23rd Nov 2021, 11:41 AM
Zura Papiashvili
Zura Papiashvili - avatar
+ 2
I want a code
24th Nov 2021, 6:54 AM
Pavan Kumar
+ 2
Pavan Kumar Code for "with" statement to work with file: with open("test.txt",'w') as f: f.write("Test file created.\n") f.write("Some text written.\n")
24th Nov 2021, 1:40 PM
Ilyas Bakirov
Ilyas Bakirov - avatar
+ 1
It's recommended to close the file after using it. f = open("demofile.txt", "r") # some code f.close()
22nd Nov 2021, 7:29 AM
Zura Papiashvili
Zura Papiashvili - avatar
+ 1
Yes but i don't understand how to create a file ??
22nd Nov 2021, 7:37 AM
Mathéo
Mathéo - avatar
+ 1
Thanks And in the course, it is written that he must be in the same file of the code (Sorry i don't speak english very well)
22nd Nov 2021, 9:06 AM
Mathéo
Mathéo - avatar
+ 1
Mathéo You Can Even Link Files In Different Directories!
22nd Nov 2021, 10:05 AM
Sancho Godinho
Sancho Godinho - avatar
0
Just do like this for open file f = open("python.txt,"r)
22nd Nov 2021, 7:09 AM
Sâñtôsh
Sâñtôsh - avatar
0
You're best bet is to use a context manager to open the file (ex. The 'with'). That would be similar to an example in the comments. with open("file.txt", 'r') as f: #your code You don't have to close the file with this method either because it's being used in a context manager and when the code you right finishes executing it will close the file for you. You can also use context managers for other things as well, such as sockets.
22nd Nov 2021, 11:57 AM
Jesse Leverett
Jesse Leverett - avatar
- 2
#your code goes here weight=int(input()) height=float(input()) if weight//height**2<18.5: print("Underweight") if weight//height**2<25: print("Normal") if weight//height**2<30: print("Over weight") if weight//height**2==30: print("Obesity") SAMPLE INPUT=85 1.9 SAMPLE OUTPUT=Normal
23rd Nov 2021, 11:17 AM
Ravi Teja
Ravi Teja - avatar
- 2
I want code
23rd Nov 2021, 11:19 AM
Ravi Teja
Ravi Teja - avatar