[IBM]Python for Data Science, AI & Development - Reading and Writing files with open

2021. 5. 7. 12:16Data science/Python

반응형

1. Reading files with open

# Read the Example1.txt

example1 = "Example1.txt"
file1 = open(example1, "r")
# Open file using with

with open(example1, "r") as file1:
    FileContent = file1.read()
    print(FileContent)
    
# Read first four characters

with open(example1, "r") as file1:
    print(file1.read(4))
    
# Read certain amount of characters

with open(example1, "r") as file1:
    print(file1.read(4))
    print(file1.read(4))
    print(file1.read(7))
    print(file1.read(15))

# Read certain amount of characters

with open(example1, "r") as file1:
    print(file1.read(16))
    print(file1.read(5))
    print(file1.read(9))
    
# Read one line

with open(example1, "r") as file1:
    print("first line: " + file1.readline())
    
with open(example1, "r") as file1:
    print(file1.readline(20)) # does not read past the end of line
    print(file1.read(20)) # Returns the next 20 chars
    
# Iterate through the lines

with open(example1,"r") as file1:
        i = 0;
        for line in file1:
            print("Iteration", str(i), ": ", line)
            i = i + 1
            
# Read all lines and save as a list

with open(example1, "r") as file1:
    FileasList = file1.readlines()

2. Writing 

# Write line to file
exmp2 = '/resources/data/Example2.txt'
with open(exmp2, 'w') as writefile:
    writefile.write("This is line A")

# Read file

with open(exmp2, 'r') as testwritefile:
    print(testwritefile.read())
    
# Write lines to file

with open(exmp2, 'w') as writefile:
    writefile.write("This is line A\n")
    writefile.write("This is line B\n")
    
##However, note that setting the mode to w overwrites all the existing data in the file.

 

Appending Files : keep the existing data by using "a" instead of "w"

# Write a new line to text file

with open('Example2.txt', 'a') as testwritefile:
    testwritefile.write("This is line C\n")
    testwritefile.write("This is line D\n")
    testwritefile.write("This is line E\n")

# Verify if the new line is in the text file

with open('Example2.txt', 'r') as testwritefile:
    print(testwritefile.read())
    
  • r+ : Reading and writing. Cannot truncate the file.
  • w+ : Writing and reading. Truncates the file. (Overwrite and delete existing data)
  • a+ : Appending and Reading. Creates a new file, if none exists. You don't have to dwell on the specifics of each mode for this lab.
#try out the a+ mode:
with open('Example2.txt', 'a+') as testwritefile:
    testwritefile.write("This is line E\n")
    print(testwritefile.read())
    
  • .tell() - returns the current position in bytes
  • .seek(offset,from) - changes the position by 'offset' bytes with respect to 'from'. From can take the value of 0,1,2 corresponding to begin, relative to current position and end
with open('Example2.txt', 'a+') as testwritefile:
    print("Initial Location: {}".format(testwritefile.tell()))  **Current location return 
    
    data = testwritefile.read()
    if (not data):  #empty strings return false in python       **If there are no more values, 
            print('Read nothing') 
    else:                                                       **If there are more to read 
            print(testwritefile.read())
            
    testwritefile.seek(0,0) # move 0 bytes from beginning.      **back to 0 location 
    
    print("\nNew Location : {}".format(testwritefile.tell()))   **check if location is 0
    data = testwritefile.read()                                 **read 
    if (not data): 									
            print('Read nothing') 
    else: 
            print(data)
    
    print("Location after read: {}".format(testwritefile.tell()) )**check if there are more to read
    
===============================  
Initial Location: 70
Read nothing

New Location : 0
Overwrite
This is line C
This is line D
This is line E
This is line E

Location after read: 70
  • .truncate() method: This will reduce the file to your data and delete everything that follows.
with open('Example2.txt', 'r+') as testwritefile:
    data = testwritefile.readlines()
    testwritefile.seek(0,0) #write at beginning of file
   
    testwritefile.write("Line 1" + "\n")
    testwritefile.write("Line 2" + "\n")
    testwritefile.write("Line 3" + "\n")
    testwritefile.write("finished\n")
    
    testwritefile.seek(0,0)
    print(testwritefile.read())
    
->Line 1
->Line 2
->Line 3
->finished
->is line D
->This is line E
->This is line E

    
# with Truncate()
with open('Example2.txt', 'r+') as testwritefile:
    data = testwritefile.readlines()
    testwritefile.seek(0,0) #write at beginning of file
   
    testwritefile.write("Line 1" + "\n")
    testwritefile.write("Line 2" + "\n")
    testwritefile.write("Line 3" + "\n")
    testwritefile.write("finished\n")
  
    testwritefile.truncate()
    testwritefile.seek(0,0)
    print(testwritefile.read())
    
->Line 1
->Line 2
->Line 3
->finished

Overwrite from the beginning. And if you use Truncate() method, the rest of the data will be deleted from the location. 

 

Copy a File

#Copy file to another 
with open('Example2.txt', 'r') as readfile:             '''readfile'''
	with open('Example3.txt', 'w') as writefile:		'''write into writefule'''
    	for line in readfile:
        	writefile.write(line)
            
#Verify if the copy is successfully executed 
with open('Example3.txt','r') as testwritefile:
	print(testwritefile.read())

# Copy the data from readfile to writefile (example 2 -> example 3)

1) Example2.txt ->readfile 

2) Example3.txt -> writefile

3) in readfile, date from readfile copy and paste in writefile  

4) Verify Example3.txt -> testwritefile 

Exercise 

Your local university's Raptors fan club maintains a register of its active members on a .txt document. Every month they update the file by removing the members who are not active. You have been tasked with automating this with your python skills.

#Run this prior to starting the exercise
from random import randint as rnd

memReg='members.txt'
exReg='inactive.txt'
fee=('yes','no')

def genFiles(current, old)
	with open(current,'w+') as writefile:
		writefile.write('Membership No Date Joined Active \n')
		data = "{:^13} {:<11} {:<6}\n"
        
		for rowno in range(20):
			date = str(rnd(2015,2020)+'-' +str(rnd(1,12))+'-'+str(rnd(1,25))
			writefile.write(data.format(rnd(10000,99999), date,fee[rnd(0,1)]))
            
	with open(old,'w+') as writefile:
		writefile.write('Membership No Date Joined Active \n')
		data = "{:^13}  {:<11}  {:<6}\n"
        
		for rowno in range(3):
			date = str(rnd(2015,2020))+'-'+str(rnd(1,12))+'-'+str(rnd(1,25))
			writefile.write(data.format(rnd(10000,99999), date,fee[1]))
            
genFiles(memReg, exReg)

Generate members.txt and inactive,txt by using randint  

def cleanFiles(currentMem, exMem):
	with open(currentMem, 'r+') as writeFile: 
    	with open(exMem, 'a+') as appendFile:
        	writeFile.seek(0)
            members=writeFile.readlines()
            header = members[0]
            members.pop(0)                      '''delete the items in the index 0'''
            
            inactive =[member for member in members if('no' in member)] '''if statement'''
            
            writeFile.seek(0)
            writeFile.write(header)
            for member in members:
            	if (member in inactive):
                	appendFile.write(member)
				else:
                	writeFule.write(member)
			writeFile.truncate()
            
memReg='members.txt'
exReg ='inactive.txt'
cleanFiles(memReg, exReg)

headers = "Membership No Date Joined Active \n"
with open(memReg, 'r') as readFile:
	print("Active Members: \n\n")
    print(readFile.read())
   
with open(exReg, 'r') as reafFile:
	print("Inactive Members" \n\n")
    print(readFile.rea())
            
            
            

cleanFile:

1) currentMem , reading and writing(not overwritten) - writeFile 

2) exMem, append and writing (not overwritten) - appendFile

3) go to writeFile location 0 (currentMem)

4) members = read each lines of writeFile(currentMem)

5) header = members[0] : header is located in members index 0 

6) members.pop(0) : delete the the item in index 0 and return rest of them(return except the header) 

7) inactive = if variable member belongs to members and no is in member 

8) go to writeFile location 0 (currentMem)

9) write header in writeFile 

10) member is in inactive, write member in appendfile(exMem) , if not, write in writeFile(currentMem) 

11) delete the rest of data in writeFile(currentMem)

 

 

github.com/IreneJeong/Python-Code 

 

 

반응형