File Management in Python
프로그래밍 2011. 8. 2. 10:33 |(Page 1 of 5 )
The next step is to write data to the file:
fileHandle.write ( 'This is a test.nReally, it is.' )
To get past this, use the "a" mode to append data to a file, adding data to the bottom:
- store the lines of a file into a list:
fileHandle = open ( 'testBinary.txt', 'rb' )
print fileHandle.read()
fileHandle.close()
File management is a basic function, and an important part of many applications. Python makes file management surprisingly easy, especially when compared
Reading and Writing
Reading and Writing
open a file for writing:
fileHandle = open ( 'test.txt', 'w' )
fileHandle = open ( 'test.txt', 'w' )
The next step is to write data to the file:
fileHandle.write ( 'This is a test.nReally, it is.' )
Finally, we need to clean up after ourselves and close the file:
fileHandle.close()
fileHandle.close()
To get past this, use the "a" mode to append data to a file, adding data to the bottom:
fileHandle = open ( 'test.txt', 'a' )
fileHandle.write ( 'nnnBottom line.' )
fileHandle.close()
fileHandle.write ( 'nnnBottom line.' )
fileHandle.close()
Read a file
Now let's read our file and display the contents:
Now let's read our file and display the contents:
- read the entire file and print the data within it.
fileHandle = open ( 'test.txt' )
print fileHandle.read()
fileHandle.close()
print fileHandle.read()
fileHandle.close()
- read a single line in the file:
fileHandle = open ( 'test.txt' )
print fileHandle.readline() # "This is a test."
fileHandle.close()
fileHandle = open ( 'test.txt' )
print fileHandle.readline() # "This is a test."
fileHandle.close()
- store the lines of a file into a list:
fileHandle = open ( 'test.txt' )
fileList = fileHandle.readlines()
for fileLine in fileList:
print '>>', fileLine
fileHandle.close()
fileList = fileHandle.readlines()
for fileLine in fileList:
print '>>', fileLine
fileHandle.close()
Adjust Offset
When reading a file, Python's place in the file will be remembered, illustrated in this example:
fileHandle = open ( 'test.txt' )
garbage = fileHandle.readline()
fileHandle.readline() # "Really, it is."
fileHandle.close()
garbage = fileHandle.readline()
fileHandle.readline() # "Really, it is."
fileHandle.close()
Only the second line is displayed. We can, however, get past this by telling Python to resume reading from a different position:
fileHandle = open ( 'test.txt' )
garbage = fileHandle.readline()
fileHandle.seek ( 0 )
print fileHandle.readline() # "This is a test."
fileHandle.close()
garbage = fileHandle.readline()
fileHandle.seek ( 0 )
print fileHandle.readline() # "This is a test."
fileHandle.close()
In the above example, we tell Python to continue reading from the first byte in the file.
Thus, the first line is printed. We can also request Python's place within the file:
Thus, the first line is printed. We can also request Python's place within the file:
fileHandle = open ( 'test.txt' )
print fileHandle.readline() # "This is a test."
print fileHandle.tell() # "17" current offset
print fileHandle.readline() # "Really, it is."
print fileHandle.readline() # "This is a test."
print fileHandle.tell() # "17" current offset
print fileHandle.readline() # "Really, it is."
It is also possible to read the file a few bytes at a time:
fileHandle = open ( 'test.txt' )
print fileHandle.read ( 1 ) # "T"
fileHandle.seek ( 4 )
print FileHandle.read ( 1 ) # "T"
print fileHandle.read ( 1 ) # "T"
fileHandle.seek ( 4 )
print FileHandle.read ( 1 ) # "T"
Binary Mode
read and write files in binary mode, such as images or executional files.
To do this, simply append "b" to the file mode:
To do this, simply append "b" to the file mode:
fileHandle = open ( 'testBinary.txt', 'wb' )
fileHandle.write ( 'There is no spoon.' )
fileHandle.close()
fileHandle.write ( 'There is no spoon.' )
fileHandle.close()
fileHandle = open ( 'testBinary.txt', 'rb' )
print fileHandle.read()
fileHandle.close()
'프로그래밍' 카테고리의 다른 글
[Android] 화면 회전시에 Activity onCreate() 방지하기 (0) | 2011.08.06 |
---|---|
[Delphi] DLL 호출 규칙(Calling Convention) 테스트용 DLL 및 Source - Language/Delphi - (0) | 2011.08.02 |
길게 터치하는 이벤트 처리 - onLongPress() (0) | 2011.07.31 |
SVN: Store password unencrypted (yes/no)? (0) | 2011.07.28 |
Visual Assist - Edit Snppet (0) | 2011.07.22 |