Skip to main content

Open , Read and Close , Print , Split , Form an orderly queue , Repeat , Just Use a Loop!

 


Reading From a File 


Once we've got data into a file, wouldn't it be just splendid to load it back into our program to use again?
Yes. Yes is the answer you're looking for.


Open 

👉 The code to load from a file is pretty similar to that for writing. The command is open instead of read, and the permissions are slightly different.


f = open("filenames.list", "r") 

 

Read and Close

👉 In this example, the permission 'r' means 'read only'. Now we load the contents of the file into a variable using the read function. Next, close the file to free up the RAM used to store it.


f = open("filenames.list", "r")
contents = f.read()
f.close()

Print

👉 Finally output the contents of the 'contents' variable to test that it worked.

f = open("filenames.list", "r")
contents = f.read()
f.close()
print(contents)

Split


Bringing everything in in one go is fine, but it would be much more useful to have it as separate items so we can examine it more easily.

👉 To do this, use the .split() function in the second to last line. This splits the string into a list of individual elements.

f = open("filenames.list", "r")
contents = f.read()
f.close()
contents = contents.split() #added split here
print(contents) 
One At A Time

Form an orderly queue

Reading all the data at once is fine, reading one item at a time works slightly differently.

It uses the .readline() function.

👉 The code below reads one line from the file.

f = open("filenames.list","r")
contents = f.readline()
print(contents)
f.close()
 
Repeat

To read more than one line, we repeat the command.

👉 The strip() function removes the default new line from each print, which would cause an empty line between each line from the file.

f = open("filenames.list","r")
contents = f.readline().strip()
print(contents)
contents = f.readline().strip()
print(contents)
contents = f.readline().strip()
print(contents)
contents = f.readline().strip()
print(contents)
f.close()


Just Use a Loop!

👉 If you're screaming 'LOOP! USE A FREAKING LOOP, MAN!' at the screen right about now.

Your wish is my command.

f = open("filenames.list","r")
while True:
  contents = f.readline().strip()
  
  if contents == "":
    break
  #The last line in the file will be a blank
  #We break the loop if the line read is a blank
  print(contents)
  # Moved the print after the break so it won't output the final blank line.
f.close()






Comments

Popular posts from this blog

WHAT IS Nesting

 WHAT IS Nesting  Nesting is where we put an  if  statement within an  if  statement using the power of indenting. The second  if  statement within the first  if  statement must be indented and its  print  statement needs to be indented one more time.  EXAMPLE:

IF AND ELSE STATEMENT

  If Statements    These statements are a bit like asking a question. You are telling the computer:  if  something is true,  then  do this specific block of code. Double equals ( == ) is asking the computer to compare if these two things are  exactly  the same.  EXAMPLE :  myName = input("What's your name?: ") if myName == "David":  What is else? IF  the condition is  not  met with the  if  statement, then we want the computer to do the  else  part instead. Likewise, if the condition  is  met in the  if  statement, then the  else  bit is ignored by the computer. The  else  statement must be the first thing  unindented  after the  if  statement and in line with it    EXAMPLE :  myName = input("What's your name?: ") if myName == "David":  print("Welcome Dude!")  print("You're just the baldest dude I've ever seen...

FOR LOOP , RANGE

  FOR LOOP  A  while  loop is perfect to use when we  don't  know how many times we want the loop to repeat.  If we have an idea of how many times we want the loop to repeat, we can use a  for  loop to loop code in exactly the same way the  while  loop did.  EXAMPLE :  for counter in range(10):   print(counter) RANGE  The  range  function creates a list of numbers in the range you create. If you only give it one number, it will start at  0  and move to a state where the final number is  one less  than the number in the brackets. In this case, the final number would be  9 .  EXAMPLE :  total = 0 for number in range(100) :   total += number   print(total)