Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special.

Similar presentations


Presentation on theme: "Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special."— Presentation transcript:

1 Python Simple file reading Peter Wad Sackett

2 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special construct for reading files in a simple manner. with open(’filename.txt’, ’r’) as infile: for line in infile: print(line) # doing something with the line # more python stuff can be done here print(”Now done with the file”) open opens the file ’filename.txt’ for reading; ’r’. The associated filehandle is here called infile, but can be anything. The for loop iterates through each line in the file. The variable is called line (appropiate), but can be anything. When done with the with statement, the file is automatically closed. Using the with statement

3 3DTU Systems Biology, Technical University of Denmark Questions to ask (yourself) when working with files Questions Did I read any numbers/lines? What happens if the input is different from the expected? Can I make input that will break my program?

4 4DTU Systems Biology, Technical University of Denmark Some answers Any variable can be assigned the value None which is different from zero (0). myvar = None if myvar is None: print(’Buuh, nothing’) if myvar is not None: print(’Yeah, something’) You test for identity by using the key words is (and is not). var1 = ’I am a string today’ var2 = ’I am a string today’ if var1 == var2:# This is true if var1 is var2:# This is false Due to an internal python quirk short strings and numbers reside the same place in memory and can trigger a true identity check.

5 5DTU Systems Biology, Technical University of Denmark Loop control When inside a loop you can exit it early with break. for i in range(10): if i == 5: break print(i) You can also go to the next iteration of the loop with continue. for i in range(10): if i == 5: continue print(i) This works with both while and for loops and you can have any number of break’s and continue’s in the loop. Generelly, break and continue should not be overused, as they lead to weaker logical thinking. Something beginners are especially prone to do. Yes, that means you.

6 6DTU Systems Biology, Technical University of Denmark Empty statement At any point where you can write a python statement, you can always use the empty statement: pass pass if var1 == var2: pass This statement does nothing and takes no time to execute. When the python syntax requires a statement and you don’t wish to ”do” anything, then you pass.


Download ppt "Python Simple file reading Peter Wad Sackett. 2DTU Systems Biology, Technical University of Denmark Simple Pythonic file reading Python has a special."

Similar presentations


Ads by Google