Working with files in python
In many cases we need to read from or write to a file. Python has simple mechanisms that we can use for that purpose.
Delete file and create new one with initiall text
def prepareOutputFile():
with open(outputFile, "w") as myfile:
myfile.write( "UserID,BusinessID\n" )
return
Append text to a file
def writeOutput( string ):
with open(outputFile, "a") as myfile:
myfile.write( string )
return
Open file and process line by line:
with open(inputFile) as f:
next(f) # skip header
for line in f:
print line
Stripping the newline character:
read a file line by line into a list
usersList = [line.rstrip('\n') for line in open(inputUsersFile)]