+ All Categories
Home > Documents > With Python. One of the most useful abilities of programming is the ability to manipulate files. ...

With Python. One of the most useful abilities of programming is the ability to manipulate files. ...

Date post: 04-Jan-2016
Category:
Upload: charles-garrison
View: 212 times
Download: 0 times
Share this document with a friend
28
File Manipulation With Python
Transcript
Page 1: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

File ManipulationWith Python

Page 2: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

One of the most useful abilities of programming is the ability to manipulate files.

Python’s operations for file management are relatively simple.

Files

Page 3: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Opening and Closing:◦ Files must always be opened before accessing,

and closed when done, or else file errors will turn up.

f = open(“data.txt”, “w+”)f.close()

The First parameter is the filename.The second parameter is the ‘mode’

Files

Page 4: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

r -> read only w -> write only a -> append r+ -> reading and writing w+ -> will create a file if it doesn’t exist Append b to the end of any mode for binary

reading/writing

Mode isn’t necessary, r will be assumed if it is not specified

File Modes

Page 5: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

File functions:◦ str = f.read()

Will read the entire file and return a string◦ str = f.readline()

Will read the next line of the file and return a string◦ f.write(str)

Will write the string str to the file◦ f.seek(4)

Will seek 4 bytes further in the file (only works in binary modes)

◦ f.read(4) Will read 4 bytes from the file (only works in binary

modes)

Files

Page 6: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Reading from a file:

This will print out the entire file

Files

Page 7: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Reading from a file:

This will print out one line at a time.

Files

Page 8: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Writing to a file:

“\n” will write a newin the file.

Files

Page 9: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Escape characters are used to write certain characters that are illegal because they are used in the language.

Escape Characters

\t Tab

\n New Line

\r Carriage Return

\” Quotes

\b Backspace

\\ Backslash

\’ Apostrophe

Page 10: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

FunctionsWith Python

Page 11: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Functions are a quick way to make multiple use of lines of code without rewriting it.

Functions

This function adds four to any passed value, and then returns the new value

Page 12: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Functions have 4 main parts:

Functions

Declaration

LabelParameters

Code

Page 13: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Declaration:◦ In Python, every function is declared using the

“def” statement.◦ This says that everything indented after this line

is a part of this function

Functions

Page 14: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Label◦ The label is how you reference a function outside

of the function.◦ Functions can have the same names as variables

but it is really bad practice to do so.

Functions

Page 15: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Parameters◦ Parameters are values that you pass into a

function◦ For Example:

I could call both addFour(3) and addFour(4) and I would get back 7 and 8 respectively

◦ You can have as many parameters as you want for any given function

◦ Variable numbers of parameters are also allowed

Functions

Page 16: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Multiple parameters◦ You can pass as many defined parameters as you

like into a function.◦ Multiple parameters are separated by a comma

Functions

Page 17: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Variable Parameters◦ When using variable

parameters you use the * to signify an array. The remaining parameters are then stored into the specified array

◦ This function adds the passed numbers together

Functions

Page 18: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Specified and Variable parameters◦ You can combine the specified parameters and

arrays to make a useful function◦ This one finds averages of a certain amount of

numbers

Functions

Page 19: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Code◦ The code in a function is what gets executed

when you call the function.◦ The return value in a function is the value that the

function is set to after it has completed.◦ The previous function x = addFour(x) will set x to

x+4 on its return

Functions

Page 20: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Recursion occurs when a function calls itself.

This is useful when you plan to scale down a parameter

Recursive functions must have a break condition that returns a set value

Recursion

Breakvalues

Page 21: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Python uses a bunch of built in functions such as:◦ print prints the parameters with a space in

between◦ len returns the length of a string or array◦ range returns a range of numbers◦ int returns integer value of a number◦ atoi returns integer value of a string◦ float returns decimal value of a number◦ Many File operations◦ All the python included functions can be found at

http://docs.python.org/library/

Functions

Page 22: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

VariablesIn Python

Page 23: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Global vs. Local variables◦ Local variables are variables used inside of

functions They cannot be referenced outside of the function.

◦ Global variables can be used while the program is still running They can be referenced across

functions

Variables

local

global

Page 24: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

All parameters that are passed into a function are automatically local variables

Local variables are useful because it allows you to reuse variable names in different functions

Local Variables

Page 25: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Global variables are any variables defined outside of a function

In Python, any variables in the main function are automatically global variables

Variables are defined as global or local when they are first called. if x is first called inside a function, it will give you an error when you try to call it in a global context.

Global Variables

Page 26: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

A B C D E X Y Z

VariablesLocalGlobalLocalLocalLocalGlobalGlobalGlobal

Page 27: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Write a program that will generate a file containing the numbers 0 – 99 (“\n” creates a new line)

Write a program that uses a defined function to multiply an arbitrary amount of numbers together.

Write a program that will print the numbers 1 through 5 recursively

Write a program that will print the first alphabetical name from a file containing a list of names

Practice

Page 28: With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.

Write a program that will count the number of lines in a file and print the result to the screen.

Write a program that will convert an entire file to lowercase letters and print it to a new file.

Write a program that uses a function to convert mass and velocity to kinetic energy. (Hint: KE = ½ mv2)

Write a recursive function that will give you the number at the nth position of the Fibonacci sequence: 0 1 1 2 3 5 8 13 21◦ (Hint: Fib(n) = Fib(n-1) + Fib(n-2))

Homework


Recommended