file = open( 'file.py' ) # readonly by default
file.close()
file = open( "/path/filename.mp3" , "rb" ) #readonly binary, only windows uses the b
print file.mode # rb
print file.name # /path/filename.mp3
f.tell() # 0 ,
f.seek( 128 , 0 ) # 0 = absolute position from start so 128 bytes from the start
f.seek( 128 , 1 ) # 1 = relative position
f.tell() # 256 bytes from the start
tagData = f.read( 128 ) # read 128 byte string , read() will get until the end of file
f.seek(-128, 2) # 2 = absolute position from the end , so -128 bytes from the end
print file.closed # False
file.close()
print file.closed # True
logfile = open( 'test.log' , 'w' ) # overwrites if already exists
logfile.write( 'my test' )
print file( 'test.log' ).read() # reads the entire file contents
# r = read, a = append, w = write (overwrites), r+ = both reading and writing, 'b' is for Windows compatibility (distinguishes between text and binary)
import csv
import fileinput
import sys
# mysql -uSER -pASSWORD exampledb -e "select * from table;" | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > filename.csv
# http://docs.python.org/2/library/csv.html
# http://docs.python.org/2.7/library/fileinput.html, http://effbot.org/librarybook/fileinput.htm
# csv module does not want any extra newlines, so cleaning them up first
for line in fileinput.input('filename.csv', inplace=1):
if line.rstrip():
sys.stdout.write(line)
# alternatively, readline() gets a single line
- - - - - - - - - - - - - - - - - - - - - - - -
# LARGE FILES USING A BUFFER
with open( 'test.log' ) as file:
for line in file: # iterable = buffered IO and memory management
do_something( line )
def read_in_chunks( fileObj , chunkSize = 2048 ): # 2KB chunks
while True:
data = fileObj.read(chunkSize)
if not data:
break
yield data
file = open( 'test.log' )
for chunk in read_in_chunks( file ):
do_something( chunk )
- - - - - - - - - - - - - - - - - - - - - - - -
import shutil
shutil.copy2('/dir/file.ext', '/new/dir') # preserves the mod, access, and file metadata
copyfile( src , dst ) # does not preserve stuff
copy( src , dst ) # destination can be a directory
print "\nwith 'open with' it closes the file when the block exits\n"
with open( "file.py") as file:
for line in file:
print line
os.tmpfile() # Return a new file object opened in update mode (w+b).
# # # # # # # # # # # #
import os.path
print "This file is named:" , __file__ , " = __file__"
print "This file's full path and name is: " , os.path.realpath( __file__ ) , " = os.path.realpath( __file__ )"
print "This file's directory:" , os.path.dirname( os.path.realpath( __file__ ) ) , " = os.path.dirname( os.path.realpath( __file__ ) ) "
print "Appending is as easy as: " , os.path.join( os.path.dirname( os.path.realpath( __file__ ) ) , 'subdirectory' ) , "os.path.join( os.path.dirname( os.path.realpath$
TEMPLATE_DIRS = (
os.path.join( os.path.dirname( os.path.realpath( __file__) ) , 'templates'),
)
print TEMPLATE_DIRS;
- - -
### Hack to extract text with BeautifulSoup
import sys
from bs4 import BeautifulSoup
if __name__ == '__main__':
if len(sys.argv) < 3:
print('ERROR, requires a source filename and keyword from <div id="myhelp"> to extract from the log, example: '
'python {} my.log myhelp'.format(sys.argv[0]))
exit(1)
filepath = sys.argv[1]
keyword_id = sys.argv[2]
try:
with open(filepath) as f:
soup = BeautifulSoup(f)
help_text = soup.find(id=keyword_id).get_text()
print(help_text)
except Exception as error:
print('ERROR: unable to extract text from {}: {}'.format(filepath, error))