Level 3 Computing Year 2 Lecturer: Phil Smith

Similar documents
Previously. Iteration. Date and time structures. Modularisation.

Previously. Iteration. Date and time structures. Modularisation.

Announcements COMP 141. Writing to a File. Reading From a File 10/18/2017. Reading/Writing from/to Files

Fundamentals of Python: First Programs. Chapter 4: Text Files

Level 3 Computing Year 2 Lecturer: Phil Smith

Scripting Languages Course 1. Diana Trandabăț

Processes. Shell Commands. a Command Line Interface accepts typed (textual) inputs and provides textual outputs. Synonyms:

Mills HPC Tutorial Series. Linux Basics I

Python for Astronomers. Week 1- Basic Python

SIMPLE I/O WITH PYTHON

CMSC 104 Lecture 2 by S Lupoli adapted by C Grasso

5/8/2012. Creating and Changing Directories Chapter 7

Level 3 Computing Year 1 Lecturer: Phil Smith

Operating System Interaction via bash

CSE 391 Lecture 1. introduction to Linux/Unix environment

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Essential Linux Shell Commands

Files on disk are organized hierarchically in directories (folders). We will first review some basics about working with them.

Python Basics. Lecture and Lab 5 Day Course. Python Basics

CSE 391 Lecture 1. introduction to Linux/Unix environment

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Chapter 6: Files and Exceptions. COSC 1436, Summer 2016 Dr. Ling Zhang 06/23/2016

1. What type of error produces incorrect results but does not prevent the program from running? a. syntax b. logic c. grammatical d.

Unix background. COMP9021, Session 2, Using the Terminal application, open an x-term window. You type your commands in an x-term window.

What is software? Functional view. Functional view. System requirements of a software. Versions Free version: This is the free version

Session 04 Introduction to Software

Lecture 23. Java File Handling. Dr. Martin O Connor CA166

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Bioinformatics? Reads, assembly, annotation, comparative genomics and a bit of phylogeny.

Computer Architecture Lab 1 (Starting with Linux)

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

FILE MAINTENANCE COMMANDS

CSE 390a Lecture 1. introduction to Linux/Unix environment

Note that the reference does not include the base directory or an initial backslash. The file extension for UI canvases should be included.

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

OPERATING SYSTEMS: Lesson 12: Directories

Lecture 3. Functions & Modules

Beyond this course. Machine code. Readings: CP:AMA 2.1, 15.4

Introduction to Python

bash Execution Control COMP2101 Winter 2019

PYTHON. Values and Variables

Working with Lists 4

CS Unix Tools & Scripting Lecture 7 Working with Stream

Using Introspect ESP Python Modules in External Python Scripts

Introduction to the Linux Command Line

Python Class-Lesson1 Instructor: Yao

Writing and Running Programs

rpaths Documentation Release 0.13 Remi Rampin

Variables, Constants, and Data Types

Lab: Supplying Inputs to Programs

CMSC201 Computer Science I for Majors

File Handling Programming 1 C# Programming. Rob Miles

Fundamentals of Python: First Programs. Chapter 4: Strings and Text Files

h/w m/c Kernel shell Application s/w user

You will automatically be in your user (home) directory when you login.

MULTIPLE CHOICE. Chapter Seven

Pathologically Eclectic Rubbish Lister

PYTHON. BBM103 Introduction to Programming Lab 1. Hacettepe University Computer Engineering Department

IntroductIon to dos. 2.1 IntroductIon. 2.2 SettIng up dos

CENG 334 Computer Networks. Laboratory I Linux Tutorial

8.1 Software/Hardware Required: Core 2 duo/i3/i5/i7-64bit processor Operating System ubuntu/fedora 64bit OS Assembler: NASM Editor Used gedit

Files and Directories

Semester 2, 2018: Lab 1

EL2310 Scientific Programming

Script Host 2.0 Developer's Guide

When working with files and directories, it is often

DOS INT 21h - DOS Function Codes

Python Programming Exercises 1

Week 2 Lecture 3. Unix

Lecture 3. Functions & Modules

Course 144 Supplementary Materials. UNIX Fundamentals

Recovering Windows Server 2003 on HP Integrity Servers

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Bashed One Too Many Times. Features of the Bash Shell St. Louis Unix Users Group Jeff Muse, Jan 14, 2009

1 Getting Started with Linux.

Individual research task. You should all have completed the research task set last week. Please make sure you hand it in today.

The UNIX Operating System. HORT Lecture 2 Instructor: Kranthi Varala

Unix Tutorial Haverford Astronomy 2014/2015

CMPS 12A Introduction to Programming Lab Assignment 7

-1- csh cd. cd alias!! ; set prompt=" pwd % " 16 cd. 17 cd.. 18 his /home% set prompt. alias. yasuoka : root :

Session 1: Accessing MUGrid and Command Line Basics

Conventions in this tutorial

TECHILA DISTRIBUTED COMPUTING ENGINE BUNDLE GUIDE

Go to the Python download page:

Lab Working with Linux Command Line

Getting Started with UNIX

Chapter 6: Files and Exceptions. COSC 1436, Spring 2017 Hong Sun 3/6/2017

Level 3 Computing Year 2 Lecturer: Phil Smith

Examples: Directory pathname: File pathname: /home/username/ics124/assignments/ /home/username/ops224/assignments/assn1.txt

L11. June 30, Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll

Introduction to Programming

Intro to Linux & Command Line

Basic UNIX Commands BASIC UNIX COMMANDS. 1. cat command. This command is used to create a file in unix. Syntax: $ cat filename

Linux Shell Script. J. K. Mandal

DATA 301 Introduction to Data Analytics Command Line. Dr. Ramon Lawrence University of British Columbia Okanagan

Why learn the Command Line? The command line is the text interface to the computer. DATA 301 Introduction to Data Analytics Command Line

Basic UNIX commands. HORT Lab 2 Instructor: Kranthi Varala

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Files and Folders What are Files and Folders Files: This is the Data, Images, and things the user makes

Getting started with the Spyder IDE

Transcription:

Level 3 Computing Year 2 Lecturer: Phil Smith

We looked at: Previously Reading and writing files. BTEC Level 3 Year 2 Unit 16 Procedural programming

Now Now we will look at: Appending data to existing files. File Handling part 2 Using the Python OS library to rename and delete files. BTEC Level 3 Year 2 Unit 16 Procedural programming

Recap Last lesson we developed python code to create and write records to files based upon user input. We then re read the records to display them in the console. We used the file mode of r read and w write

Adding data Now we want to extend an existing file with more data. For this we need to open the file with a new mode R read W write? append

Task 1 Start pycharm Open your code file for writing and reading files. Now follow my lead.

Reading and writing files. Some of the main Python file modes. Mode r w a Description Open a file for reading only. The file cannot be changed or written to. Open a file for writing. If the file already exists, erase its contents. If it does not exist, create it. Open a file to be written to. All data written to the file will be appended to its end. If the file does not exist, create it. We shall now learn how to specify files in other folders.

Reading and writing files. If you want to open a file in a different location, you can specify a path as well as a filename in the argument that you pass to the open function. However If you specify a path in a string literal (particularly on a Windows computer), be sure to prefix the string with the letter r. Here is an example: customer_file = open(r'c:\users\phil\customers.txt', 'r')

Reading and writing files. customer_file = open(r'c:\users\phil\customers.txt', 'r') The r prefix specifies that the string is a raw string. This causes the Python interpreter to read the backslash characters as literal backslashes. Without the r prefix, the interpreter would assume that the backslash characters were part of escape sequences, and an error would occur.

Writing Data to a File Do the (file_write.py) program in the Lab07 (on the wiki) WHAT does the \n do?

Reading Data From a File Quite often a program must read the contents of a file without knowing the number of items that are stored in the file. This presents a problem if you want to write a program that processes all of the items in the file, however many there are. We know how to read lines from a file We could use a loop to read all of the items in the file, but you need a way of knowing when the end of the file has been reached.

Reading end of File In Python, the readline method returns an empty string ('') when it has attempted to read beyond the end of a file. This makes it possible to write a while loop that determines when the end of a file has been reached. Here is the general algorithm, in pseudo code: Open the file Use readline to read the first line from the file While the value returned from readline is not an empty string: Process the item that was just read from the file Use readline to read the next line from the file. EndWhile Close the file

Reading Data From a File NOTE: In this algorithm we call the readline method just before entering the while loop. The purpose of this method call is to get the first line in the file, so it can be tested by the loop. This initial read operation is called a priming read.

Reading Data flowchart Shape used for input (read) and output (write)

Using the OS with files To rename and delete files we need to use a library which contains the methods to this. The Python OS module provides methods that help you perform file processing operations, such as renaming and deleting files. Task 2 Follow my lead

Using the OS with files Other useful methods are mkdir() to create directories. chdir() to change the current directory (from the default) getcwd() to display the current working directory. rmdir() to remove directories

Summary what we have done today Appending data to files. Using files in other folders. Using the OS library to rename and delete files.

Next Time Design with pseudo code and assignment 2