Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh 2010-2011 This work is licensed under the Creative Commons Attribution.

Slides:



Advertisements
Similar presentations
Input and Output Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advertisements

Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
By: Tony Andrews.  Linux directory ordering system  Navigating and creating directories ◦ Listing directories and files ◦ Creating directories ◦ Changing.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Control Flow Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
1 Drawing phylogenies We've seen several tree data structures but we still can't draw a tree  In today's first exercise we write a drawtree module for.
1 Drawing phylogenies We've seen several tree data structures but we still can't draw a tree  In the tree drawing exercise we write a drawtree function.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
1 SEEM3460 Tutorial Unix Introduction. 2 Introduction What is Unix? An operation system (OS), similar to Windows, MacOS X Why learn Unix? Greatest Software.
Week 2 File Systems & Unix Commands. File System Hierarchy.
1 THE UNIX FILE SYSTEM By Chokechai Chuensukanant ID COSC 513 Operating System.
Unix Primer. Unix Shell The shell is a command programming language that provides an interface to the UNIX operating system. The shell is a “regular”
IDK0040 Võrgurakendused I harjutus 06: PHP: Introduction Deniss Kumlander.
4 1 Operating System Activities  An operating system is a type of system software that acts as the master controller for all activities that take place.
INTERNET APPLICATION DEVELOPMENT For More visit:
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Includes and Dates.
Nael Alian Introduction to PHP
Internet of Things with Intel Edison Compiling and running Pierre Collet Intel Software.
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Operating Systems Programs for performing surgery NOT! –Sorry I got carried away.
Putting Applets into Web Pages.  Two things are involved in the process of putting applets onto web pages ◦ The.class files of the applet ◦ The html.
INTRODUCTION TO LINUX Jacob Chan. GNU/Linux Consists of Linux kernel, GNU utilities, and open source and commercial applications Works like Unix –Multi-user.
Linux Operations and Administration
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Windows Listening Guide.  The software that manages the sharing of the resources of a computer. The overall function of the computer.  MASTER CONTROLLER.
Operating System What is an Operating System? A program that acts as an intermediary between a user of a computer and the computer hardware. An operating.
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
File Input and Output July 2nd, Inputs and Outputs Inputs Keyboard Mouse storage(hard drive) Networks O utputs Graphs Images Videos(image stacks)
Files and Directories Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Unix Shell Basics Edited from Greg Wilson's "Software Carpentry"
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
Python – reading and writing files. ??? ???two ways to open a file open and file ??? How to write to relative and absolute paths?
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,
Querying Directory Contents Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See
Finding Things Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
PHP File Manipulation. File Upload and php.ini ;;;;;;;;;;;;;;; ; File Uploads ; ;;;;;;;;;;;;;;;; ; Whether to allow HTTP file uploads. file_uploads =
Isecur1ty training center Presented by : Eng. Mohammad Khreesha.
Exceptions Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basics Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Manipulating Directories and Files Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See.
15 – PHP(5) Informatics Department Parahyangan Catholic University.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Commands Basic syntax of shell commands UNIX or shell commands have a basic structure command -options target command comes first (such as cd or ls) any.
CSC 352– Unix Programming, Spring 2016, Final Exam Guide
Distributed Computing Systems
Tuples and Lists.
The Command Prompt Commands are the way to “do things” in Unix
Program Design Invasion Percolation: Neighbors
Introduction to Scripting
PHP Introduction.
The Linux Command Line Chapter 2
Guide To UNIX Using Linux Third Edition
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
Version Control Basic Operation Copyright © Software Carpentry 2010
Operating System Fundamentals
CISC101 Reminders Assn 3 sample solution is posted.
What is Unix? A multi-user networked operating system
OPERATING SYSTEM B-TECH III YEAR I SEM BRANCH :ECE
Files – part 2 Alexandra Stefan.
Input and Output Python3 Beginner #3.
Browsing Directories Using walk
Presentation transcript:

Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution License See for more information. Python

Directory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' planets.txt data vladusers We want to build a path

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data planets.txt data vladusers Use string addition

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data Code assumes Linux/UNIX paths

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join >>> path = join(base, user, datadir)

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join >>> path = join(base, user, datadir) >>> print path /users/vlad/data

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = base + '/' + user + '/' + datadir >>> print path /users/vlad/data >>> from os.path import join >>> path = join(base, user, datadir) >>> print path /users/vlad/data join picks the file separator based on the current operating system

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data Running under Windows

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data Running under Windows join chooses the Windows separator The double \ is only because we’re printing them

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data Running under Windows join chooses the Windows separator The double \ is only because we’re printing them But it starts with a Linux/UNIX file separator How do we convert that?

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data >>> from os.path import normpath

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data >>> from os.path import normpath >>> nupath = normpath(path) >>> print nupath \\users\\vlad\\data

PythonDirectory and File Paths >>> base = '/users' >>> user = 'vlad' >>> datadir = 'data' >>> path = join(base, user, datadir) >>> print path /users\\vlad\\data >>> from os.path import normpath >>> nupath = normpath(path) >>> print nupath \\users\\vlad\\data >>> normpath('/some/other/path') \\some\\other\\path normpath converts path separators

PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' normpath not only converts path separators

PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators

PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators, it removes duplicated path separators

PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators, it removes duplicated path separators and “.” current directory short-hand

PythonDirectory and File Paths >>> path = '/users/vlad//.///data/../..//vlad/./data/..' >>> cleanpath = normpath(path) >>> print cleanpath /users/vlad normpath not only converts path separators, it Removes duplicated path separators Removes “.” current directory short-hand Tries to resolve “..” parent directory short-hand

PythonDirectory and File Paths >>> from os.path import dirname, basename

PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' planets.txt data vladusers

PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data planets.txt data vladusers

PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt data vladusers

PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt >>> from os.path import split >>> (head, tail) = split(path) planets.txt data vladusers

PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt >>> from os.path import split >>> (head, tail) = split(path) >>> print head /users/vlad/data planets.txt data vladusers

PythonDirectory and File Paths >>> from os.path import dirname, basename >>> path = '/users/vlad/data/planets.txt' >>> dirname(path) /users/vlad/data >>> basename(path) planets.txt >>> from os.path import split >>> (head, tail) = split(path) >>> print head /users/vlad/data >>> print tail planets.txt data vladusers

PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt'

PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt' >>> (root, ext) = splitext(path)

PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt' >>> (root, ext) = splitext(path) >>> print root /users/vlad/data/planets

PythonDirectory and File Paths >>> from os.path import splitext >>> path = '/users/vlad/data/planets.txt' >>> (root, ext) = splitext(path) >>> print root /users/vlad/data/planets >>> print ext.txt

PythonDirectory and File Paths >>> from os.path import splitdrive >>> path = 'C:\\users\\vlad\\data\\planets.txt' >>> (drive, tail) = splitdrive(path)

PythonDirectory and File Paths >>> from os.path import splitdrive >>> path = 'C:\\users\\vlad\\data\\planets.txt' >>> (drive, tail) = splitdrive(path) >>> print drive C:

PythonDirectory and File Paths >>> from os.path import splitdrive >>> path = 'C:\\users\\vlad\\data\\planets.txt' >>> (drive, tail) = splitdrive(path) >>> print drive C: >>> print tail \\users\\vlad\\data\\planets.txt

PythonDirectory and File Paths >>> path = 'data/planets.txt' planets.txt data

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False planets.txt data isabs checks is the path is absolute

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False isabs checks is the path is absolute Linux/UNIX: does it start with / ? Windows: does it start with \ after the drive’s been removed? planets.txt data

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) planets.txt data vladusers abspath makes relative paths absolute

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt planets.txt data vladusers abspath makes relative paths absolute

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt planets.txt data vladusers abspath makes relative paths absolute It uses getcwd()

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True planets.txt data vladusers

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True >>> abspath('data/../..') data..

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True >>> abspath('data/../..') /users data vladusers..

PythonDirectory and File Paths >>> path = 'data/planets.txt' >>> from os.path import isabs >>> isabs(path) False >>> from os.path import abspath >>> nupath = abspath(path) >>> print abspath /users/vlad/data/planets.txt >>> isabs(nupath) True >>> abspath('data/../..') /users data vladusers abspath also normalizes the path

PythonDirectory and File Paths Beware! None of these operations check whether the directories or files exist

PythonDirectory and File Paths Beware! None of these operations check whether the directories or files exist Remember os.path exists function

PythonDirectory and File Paths os.path Common pathname manipulations join Join relative paths together using operating system- specific separators normpath Clean up a path and convert separators to be consistent with the current operating system dirname Get the path up to the final directory/file in the path basename Get the final directory/file in the path split Split a path into directory and file name splitext Split a path to get a file extension splitdrive Split a path to get a drive name isabs Is a path relative or absolute? abspathConvert a path to an absolute path, using getcwd()

May 2011 created by Mike Jackson and Greg Wilson Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution License See for more information.