-
[Python tutorial] 25. File Methods코딩/Python 2022. 12. 21. 17:30728x90
file.close()
Closes the file
f = open("demofile.txt", "r") print(f.read()) f.close()
file.fileno()
Returns a number that represents the stream, from the operating system's perspective
f = open("demofile.txt", "r") print(f.fileno())
file.flush()
Flushes the internal buffer
f = open("myfile.txt", "a") f.write("Now the file has one more line!") f.flush() f.write("...and another one!")
file.isatty()
Returns whether the file stream is interactive or not
f = open("demofile.txt", "r") print(f.isatty())
file.read()
Returns the file content
f = open("demofile.txt", "r") print(f.read())
file.readable()
Returns whether the file stream can be read or not
f = open("demofile.txt", "r") print(f.readable())
file.readline(size)
Returns one line from the file
f = open("demofile.txt", "r") print(f.readline())
file.readlines(hint)
Returns a list of lines from the file
f = open("demofile.txt", "r") print(f.readlines())
file.seek(offset)
Change the file position
f = open("demofile.txt", "r") f.seek(4) print(f.readline())
file.seekable()
Returns whether the file allows us to change the file position
f = open("demofile.txt", "r") print(f.seekable())
file.tell()
Returns the current file position
f = open("demofile.txt", "r") print(f.tell())
file.truncate(size)
Resizes the file to a specified size
f = open("demofile2.txt", "a") f.truncate(20) f.close() #open and read the file after the truncate: f = open("demofile2.txt", "r") print(f.read())
file.writable()
Returns whether the file can be written to or not
f = open("demofile.txt", "a") print(f.writable())
file.write(byte)
Writes the specified string to the file
f = open("demofile2.txt", "a") f.write("See you soon!") f.close() #open and read the file after the appending: f = open("demofile2.txt", "r") print(f.read())
file.writelines(list)
Writes a list of strings to the file
f = open("demofile3.txt", "a") f.writelines(["See you soon!", "Over and out."]) f.close() #open and read the file after the appending: f = open("demofile3.txt", "r") print(f.read())
728x90'코딩 > Python' 카테고리의 다른 글
[Python/Module] Datetime (1) 2022.12.21 [Python tutorial] 26. Keyword (0) 2022.12.21 [Python tutorial] 24. Set Methods (0) 2022.12.21 [Python tutorial] 23. Tuple Methods (0) 2022.12.21 [Python tutorial] 22. Dictionary methods (0) 2022.12.21