/
Python:  File Management Python:  File Management

Python: File Management - PowerPoint Presentation

inventco
inventco . @inventco
Follow
342 views
Uploaded On 2020-06-15

Python: File Management - PPT Presentation

Damian Gordon File Management Weve seen a range of variable types Integer Variables Real Variables Character Variables String Variables Arrays Linked Lists X 5 File Management The only problem with variables is that once the program has finished running they cease to exist and all ID: 777495

pointer file management program file pointer program management txt print open close python34 read line mydata message characters mydata2

Share:

Link:

Embed:

Download Presentation from below link

Download The PPT/PDF document "Python: File Management" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Python: File Management

Damian Gordon

Slide2

File Management

We’ve seen a range of variable types:

Integer Variables

Real VariablesCharacter Variables String VariablesArraysLinked Lists

X

5

Slide3

File Management

The only problem with variables is that once the program has finished running, they cease to exist and all the values they had are forgotten.

In formal terms “

variables only persist while the program is running”.

Slide4

File Management

It would be good if there were some way to recall some values beyond the persistence of the programs.

We can do this with FILES.

Slide5

File Management

We can WRITE data (and variables) to a file to permanently store them, and we can READ the data into other programs.

Imagine opening a Notepad file and typing the values of the variables you wish to store into the file, that’s what we can do in Python.

Slide6

File Management

We’ll start off with

READing

from a file, so let’s assume we’ve created a file already in Notepad, and we are using Python to read the values out of it.We’ll call the file MyData.txt.

Slide7

File Management

Python is a widely used high-level, general-purpose, interpreted,

dynamic

programming language. Its design philosophy emphasizes

code readability, and its syntax allows programmers to express concepts in fewer lines of code than would be possible in languages

such as C++ or Java. The language provides constructs intended to enable clear programs on both a small and large scale.

MyData.txt

Slide8

Reading Text Files

Slide9

File Management

We use the

open()

and the read()

commands:

Slide10

File Management

# PROGRAM

FileReader1

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.read())

file_pointer.close

()

#

END.

Slide11

File Management

# PROGRAM FileReader1

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.read

())file_pointer.close

()

# END.

This program opens a file called

MyData.txt

for

READing

, and prints out the whole file.

Slide12

File Management

# PROGRAM

FileReader2

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.read(20

))

file_pointer.close

()

# END.

Slide13

File Management

# PROGRAM FileReader2

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.read

(20))

file_pointer.close

()

# END.

This program opens a file called

MyData.txt

for

READing

, and prints out the first 20 characters from the file.

Slide14

File Management

# PROGRAM

FileReader3

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.read(20))

print(

file_pointer.read

(20

))

file_pointer.close

()

# END.

Slide15

File Management

# PROGRAM

FileReader3

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.read(20))

print(

file_pointer.read

(20

))

file_pointer.close

()

# END.

This program opens a file called

MyData.txt

for

READing

, and prints out the first 20 characters from the file, and then it prints out the next 20 characters.

Slide16

File Management

# PROGRAM

FileReader4

PathName

= "C:\\Python34\\"

NameOfFile =

str

(input("What File would you like to read

:"))

Extension

= ".txt"

FullFileName

=

PathName

+

NameOfFile

+

Extension

NumberOfChars

=

int

(input("How many

characters:

"))

file_pointer

= open(

FullFileName

, "r")

print(

file_pointer.read

(

NumberOfChars

))

file_pointer.close

()

#

END.

Slide17

File Management

# PROGRAM FileReader4

PathName

= "C:\\Python34\\"

NameOfFile =

str(input("What File would you like to read:"))Extension = ".txt"

FullFileName

=

PathName

+

NameOfFile

+ Extension

NumberOfChars

=

int

(input("How many characters: "))

file_pointer

= open(

FullFileName

, "r")

print(

file_pointer.read

(

NumberOfChars

))

file_pointer.close

()

# END.

This program asks the user for a filename and a number of characters, and it opens the specified file for

READing

, and prints out the specified number of characters from the file.

Slide18

File Management

# PROGRAM FileReader4

PathName

= "C:\\Python34\\"

NameOfFile =

str(input("What File would you like to read:"))Extension = ".txt"

FullFileName

=

PathName

+

NameOfFile

+ Extension

NumberOfChars

=

int

(input("How many characters: "))

file_pointer

= open(

FullFileName

, "r")

print(

file_pointer.read

(

NumberOfChars

))

file_pointer.close

()

# END.

This program asks the user for a filename and a number of characters, and it opens the specified file for

READing

, and prints out the specified number of characters from the file.

>>>

What

File would you like to read:

MyData

How many characters do you want to

print:

43

Python

is a widely used high-level,

general

Slide19

File Management

Now let’s look at the

open()

command used in conjunction with the readline

() command:

Slide20

File Management

# PROGRAM

FileReader5

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.readline())

file_pointer.close

()

# END.

Slide21

File Management

# PROGRAM

FileReader5

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.readline())

file_pointer.close

()

# END.

This program opens a file called

MyData.txt

for

READing

, and prints out the first line only of the file.

Slide22

File Management

# PROGRAM

FileReader6

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.readline(100

))

file_pointer.close

()

# END.

Slide23

File Management

# PROGRAM

FileReader6

file_pointer

= open("C:\Python34\MyData.txt", "r")

print(file_pointer.readline(100

))

file_pointer.close

()

# END.

This program opens a file called

MyData.txt

for

READing

, and prints out the first 100 characters from the first line only of the file (if there is less than 100 characters, it keeps on printing until it reaches the end of the line).

Slide24

File Management

# PROGRAM

FileReader7

file_pointer

= open("C:\Python34\MyData.txt", "r")

for line in file_pointer

:

# DO

print(line)

# ENDFOR

;

file_pointer.close

()

#

END.

Slide25

File Management

# PROGRAM

FileReader7

file_pointer

= open("C:\Python34\MyData.txt", "r")

for line in file_pointer:

# DO

print(line)

# ENDFOR

;

file_pointer.close

()

# END.

This program opens a file called

MyData.txt

for

READing

, prints out the whole file line by line.

Slide26

Writing Text Files

Slide27

File Management

To WRITE to a file we use the

open()

and the write()

commands:

Slide28

File Management

# PROGRAM

FileWriter1

file_pointer

= open("C:\Python34\MyData2.txt", "w")

print(file_pointer.write("This is a new message"))

file_pointer.close

()

# END.

Slide29

File Management

# PROGRAM

FileWriter1

file_pointer

= open("C:\Python34\MyData2.txt", "w")

print(file_pointer.write("This is a new message"))

file_pointer.close

()

# END.

This program opens a file called

MyData2.txt

for

WRITing

, and creates a new file if there isn’t one there, or overwrites the text in the file if it exists.

Slide30

File Management

# PROGRAM

FileWriter2

file_pointer

= open("C:\Python34\MyData2.txt", "w")print(file_pointer.write

("This is a new message\n"))

print(

file_pointer.write

("This is a second message\n"))

file_pointer.close

()

# END.

Slide31

File Management

# PROGRAM

FileWriter2

file_pointer

= open("C:\Python34\MyData2.txt", "w")

print(file_pointer.write("This is a new message\n"))

print(

file_pointer.write

("This is a second message\n"))

file_pointer.close

()

# END.

This program opens a file called

MyData2.txt

for

WRITing

, and creates a new file if there isn’t one there, or overwrites the text in the file if it

exists with the two lines specified in the program.

Slide32

File Management

# PROGRAM

FileWriter3

Message = ["line 1\n", "line 2\n", "line 3\n"]

file_pointer = open("C:\Python34\MyData2.txt", "w")

print(file_pointer.writelines(Message))

file_pointer.close

()

# END.

Slide33

File Management

# PROGRAM FileWriter3

Message = ["line 1\n", "line 2\n", "line 3\n"]

file_pointer

= open("C:\Python34\MyData2.txt", "w")print(

file_pointer.writelines(Message))file_pointer.close

()

# END.

This program opens a file called

MyData2.txt

for

WRITing

, and creates a new file if there isn’t one there, or overwrites the text in the file if it

exists with the three lines specified in the program.

Slide34

File Management

# PROGRAM

FileWriter4

Message = ["line 1\n", "line 2\n", "line 3\n"]

file_pointer = open("C:\Python34\MyData2.txt",

“a")print(

file_pointer.writelines

(Message))

file_pointer.close

()

# END.

Slide35

File Management

# PROGRAM

FileWriter4

Message = ["line 1\n", "line 2\n", "line 3\n"]

file_pointer

= open("C:\Python34\MyData2.txt", “a")

print(

file_pointer.writelines

(Message))

file_pointer.close

()

# END.

Does the same as the previous program, except instead of overwriting the existing text in the file, it appends the new three lines into the file.

Current File

Message

Slide36

File Management

# PROGRAM FileWriter5

file_pointer

= open("C:\Python34\MyData2.txt", "r+")

Current_file =

file_pointer.read()

New_file

= "Start of file\n" +

Current_file

file_pointer.seek

(0) # This resets the pointer to the start

file_pointer.write

(

New_file

)

file_pointer.close

()

# END.

Slide37

File Management

# PROGRAM FileWriter5

file_pointer

= open("C:\Python34\MyData2.txt", "r+")

Current_file =

file_pointer.read()New_file

= "Start of file\n" +

Current_file

file_pointer.seek

(0) # This resets the pointer to the start

file_pointer.write

(

New_file

)

file_pointer.close

()

# END.

This adds the line “Start of file” to the start of the file.

Current File

Message

Slide38

Reading Binary Files

Slide39

File Management

Let’s look at

READing

a BINARY file using the open() and the read()

commands:

Slide40

File Management

# PROGRAM

FileBinReader

file_pointer

= open("C:\Python34\Python.gif", "br

")first4 = tuple(

file_pointer.read

(4

))

if

first4 == (0x47, 0x49, 0x46, 0x38):

# THEN

print("This is a GIF file")

else:

print("This is not a GIF file")

# ENDIF

;

file_pointer.close

()

# END.

Slide41

File Management

# PROGRAM

FileBinReader

file_pointer

= open("C:\Python34\Python.gif", "br

")first4 = tuple(

file_pointer.read

(4

))

if

first4 == (0x47, 0x49, 0x46, 0x38):

# THEN

print("This is a GIF file")

else:

print("This is not a GIF file")

# ENDIF

;

file_pointer.close

()

# END.

This checks if the file specified is a GIF file or not. If it is a GIF it will start with

HEX values 0x47, 0x49, 0x46,

0x38.

Slide42

etc.