Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls.

Slides:



Advertisements
Similar presentations
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Advertisements

L which include file needs to be used for string manipulation? what using statement needs to be included fro string manipulation? l how is a string assigned.
Chapter 7 Strings F Processing strings using the String class, the StringBuffer class, and the StringTokenizer class. F Use the String class to process.
A Crash Course Python. Python? Isn’t that a snake? Yes, but it is also a...
Introduction to Computing Using Python File I/O  File Input/Output  read(), readline(), readlines()  Writing to a file.
Python’s input and output Chenghao Wang. Fancier Output Formatting – Output method ▪Print() ▪Str() ▪Repr() Example S=“Hello World” Print(s) OR Print(“Hello.
Computer Science 111 Fundamentals of Programming I Files.
File Handling Advanced Higher Programming. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a.
Random Access Files Sandeep Patel. Random Access Files Random access files are files in which records can be accessed in any order –Also called direct.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Introduction to Python
Lecture 06 – Reading and Writing Text Files.  At the end of this lecture, students should be able to:  Read text files  Write text files  Example.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
16. Python Files I/O Printing to the Screen: The simplest way to produce output is using the print statement where you can pass zero or more expressions,
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Files in Python Output techniques. Outputting to a file There are two ways to do this in Python – print (more familiar, more flexible) – write (more restrictive)
File IO.  File Input/Output  StreamWriter  StreamReader  Text Files  Binary Files.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
FILES. open() The open() function takes a filename and path as input and returns a file object. file object = open(file_name [, access_mode][, buffering])
Input and Output in python Josh DiCristo. How to output numbers str()  You can put any variable holding a number inside the parentheses and it will display.
Lecture 4 Python Basics Part 3.
File I/O Ruth Anderson UW CSE 140 Winter File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
Lecture 14 Arguments, Classes and Files. Arguments.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Lakshit Dhanda. Output Formatting Python has ways to convert any value to a string. 2 Methods repr() – meant to generate representations of values read.
Kanel Nang.  Two methods of formatting output ◦ Standard string slicing and concatenation operations ◦ str.format() method ie. >>> a = “The sum of 1.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Introduction to Python Lesson 2a Print and Types.
File I/O. I/O Flags Flags are passed to give some information about how the file is to be used. – Read only file – flag=0x0 – Write only file – flag=0x1.
Chapter 14: Sequential Access Files
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
File I/O File input/output Iterate through a file using for
Computer Programming Fundamentals
CSC 131: Introduction to Computer Science
Python’s input and output
Binary Files.
Ruth Anderson UW CSE 160 Winter 2016
File input and output Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes from 2009: Sample problem.
Binary Files.
Ruth Anderson UW CSE 160 Winter 2017
Python I/O.
Lecture 13 Input/Output Files.
Ruth Anderson UW CSE 160 Spring 2018
For loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble Notes for 2010: I skipped slide 10. This is.
Topics Introduction to File Input and Output
File IO and Strings CIS 40 – Introduction to Programming in Python
Fundamentals of Programming I Files
File I/O File input/output Iterate through a file using for
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
File I/O File input/output Iterate through a file using for
Random File Access CHAPTER 7.
While loops Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Strings and Serialization
Files Handling In today’s lesson we will look at:
C Programming Getting started Variables Basic C operators Conditionals
Topics Introduction to File Input and Output
C++ Programming: chapter 6 – iostream
COMPUTER PROGRAMMING SKILLS
Variables and Computer Memory
Lecture 18 Python OOP.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Topics Introduction to File Input and Output
File Handling.
Presentation transcript:

Python focus – files The open keyword returns a file object Opening a file myFile = open('C:\file.txt', arg) Optional argument The second argument controls whether we: 'r' : read from the file 'w' : write to the file 'r+': read and write to the file 'a' : append to the file The default mode is 'r'. Also, add a 'b' (eg, 'rb' ) if you intend to write to a binary file

Python focus – files File objects have many useful methods Working with file objects myFile.read(size) : read size characters into string myFile.readline() : read one line into string myFile.list() : read all lines into a list of strings We can iterate over lines in a file object! The above will read in each line in turn until EOF for line in myFile: print line

Python focus – files File objects specifically write strings Writing to file objects myFile.write('Wombats are best\n') myVar = 100 myFile.write(str(myVar)) Use the str function to convert other types to strings a = str(['A','C','G','T'], 99) Advanced: structured data can be saved with json

Python focus – files tell and seek relate to positions in a file Miscellaneous file methods myPosition = myFile.tell() print myPosition # integer w/ current place myFile.seek(100) # go to the 100 th character myFile.seek(offset, from) from can take the values: 0 : beginning of file 1 : current file position 2 : end of file

Python focus – files close will release memory Tidying up myFile.close() print myFile.closed() # should print True