18 ответов
+ 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")
+ 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")
+ 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/
+ 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()
+ 3
АлКа Релова You Can Run This Keyword For Help With File Handling...
>>help(open)
+ 3
АлКа Релова also look at "with" statement to work with files
+ 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()
+ 2
Can you send your task? I might suggest something then
+ 2
I want a code
+ 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")
+ 1
It's recommended to close the file after using it.
f = open("demofile.txt", "r")
# some code
f.close()
+ 1
Yes but i don't understand how to create a file ??
+ 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)
+ 1
Mathéo
You Can Even Link Files In Different Directories!
0
Just do like this for open file
f = open("python.txt,"r)
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.
- 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
- 2
I want code