Manipulating Directories and Files Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See.

Slides:



Advertisements
Similar presentations
Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Advertisements

Operating System Type of Operating System
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 2)
Exploring the UNIX File System and File Security
Linux+ Guide to Linux Certification, Second Edition
6/24/2015B.RamamurthyPage 1 File System B. Ramamurthy.
CS 497C – Introduction to UNIX Lecture 12: - The File System Chin-Chih Chang
7/15/2015B.RamamurthyPage 1 File System B. Ramamurthy.
Embedded Programming and Robotics Lesson 13 Basic Linux 1.
CS 141 Labs are mandatory. Attendance will be taken in each lab. Make account on moodle. Projects will be submitted via moodle.
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Learning basic Unix command IT 325 operating system.
File Systems Sources and Resources: 1. A Students Guide to UNIX, by Hahn 2. Paula Davidson’s Handout on UNIXHandout on UNIX.
© Crown copyright Met Office An Introduction to Linux PRECIS Workshop, University of Reading, 23rd – 27th April 2012.
Lesson 7-Creating and Changing Directories. Overview Using directories to create order. Managing files in directories. Using pathnames to manage files.
1 Lecture 2 Working with Files and Directories COP 3344 Introduction to UNIX.
Linux+ Guide to Linux Certification, Second Edition
Ch 41 Program Files, Data Files, and Subdirectories.
Chapter Two Exploring the UNIX File System and File Security.
Browsing Directories Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Managing Files. Module 5 Managing Files ♦ Introduction “On a Linux system, everything is a file; if something is not a file, it is a process.” ♦ Topics.
Second edition Your UNIX: The Ultimate Guide Das © 2006 The McGraw-Hill Companies, Inc. All rights reserved. UNIX Commands cal – will print a calendar.
1.  Microsoft DOS (Disk Operating System) use a command line user interface.command line  A command line user interface means that the user is required.
Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character.
E X C E E D I N G E X P E C T A T I O N S Basic LINUX Linux System Administration Dr. Hoganson Kennesaw State University Operating Systems Directory structure:
Linux+ Guide to Linux Certification, Third Edition
Linux+ Guide to Linux Certification, Third Edition
1 © 2001 John Urrutia. All rights reserved. Chapter 4 The LINUX Filesystem.
CS252: Systems Programming Ninghui Li Slides by Prof. Gustavo Rodriguez-Rivera Topic 7: Unix Tools and Shell Scripts.
Λειτουργικά Συστήματα – Lab2 Γιάννης Πετράκης. Directory Navigation and Control  The Unix file system is set up like a tree branching out from the root.
Linux Commands C151 Multi-User Operating Systems.
Querying Directory Contents Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See
File Systems, telnet and ftp Sources and Resources: 1. A Students Guide to UNIX, by Hahn 2. Paula Davidson’s Handout on UNIXHandout on UNIX.
1 Lecture 2 Working with Files and Directories COP 3353 Introduction to UNIX.
Intro. To Unix commands For those who’ve never used Unix before Quick tutorial to let you move around your Unix Accounts No discussion of inner workings.
Isecur1ty training center Presented by : Eng. Mohammad Khreesha.
Files and Directories in UNIX The first file in UNIX file system is “root” or “/”
UNIX filesystem CS 2204 Class meeting 2 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Guide to Parallel Operating Systems with Windows 7 and Linux Chapter 6 Directory Commands.
Linux A practical introduction. 1)Background and Getting Started Linux is an operating system with multiple providers Red Hat/CentOS (our version) Ubuntu.
File System Security ls -l. First Columm d = directory l = symbolic link b = block special file c = character special file p = fifo (or named pipe) special.
CSCI 330 UNIX and Network Programming Unit II Basic UNIX Usage: File System.
Basic Unix Commands. Listing files and directories ● ls:command is used to list the files and ● directories in present working directory ● ls command.
The Unix File System R Bigelow. The UNIX File System The file system refers to the way in which UNIX implements files and directories. The UNIX file system.
Learning basic Unix command It 325 operating system.
1.3 System Call. System Call System calls provide the interface between a running program and the operating system. System call is a method by which a.
File Management commands cat Cat command cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out).
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Learning Unix/Linux Based on slides from: Eric Bishop.
IST 222 Day 6. DOS Naming Conventions A filename contains up to 8 characters, a separating period, and a file extension of up to three characters This.
Linux Filesystem Management
UNIX Basics Matt Hayward October 18, 2016 LS560 – Information Technology for information professionals.
Intro to Python Programming – Part III
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.
USING PYTHON to Automate data management tasks
Lecture 2 Working with Files and Directories
Some Linux Commands.
The Command Prompt Commands are the way to “do things” in Unix
Useful Linux Commands.
OS The os module allows interaction with the Operating System, either generically or specific to a particular OS.
Introduction to GNU/Linux (Fedora) Command Line Interface
Introduction to UNIX.
Exploring the UNIX File System and File Security
File System B. Ramamurthy B.Ramamurthy 11/27/2018.
Operating Systems and Using Linux
The Unix File System.
UNIX/LINUX Commands Using BASH Copyright © 2017 – Curt Hill.
Module 6 Working with Files and Directories
Browsing Directories Using walk
January 26th, 2004 Class Meeting 2
Presentation transcript:

Manipulating Directories and Files Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See for more information. Python

Manipulating Directories and Files >>> from os import mkdir >>> mkdir('data') user

PythonManipulating Directories and Files >>> from os import mkdir >>> mkdir('data') userdata

PythonManipulating Directories and Files >>> from os import mkdir >>> mkdir('data') >>> listdir(getcwd()) ['data'] userdata

PythonManipulating Directories and Files >>> from os import mkdir >>> mkdir('data') >>> listdir(getcwd()) ['data'] >>> listdir('data') [] userdata

PythonManipulating Directories and Files >>> from os import mkdir >>> mkdir('data') >>> listdir(getcwd()) ['data'] >>> listdir('data') [] >>> mkdir('data') userdata

PythonManipulating Directories and Files >>> from os import mkdir >>> mkdir('data') >>> listdir(getcwd()) ['data'] >>> listdir('data') [] >>> mkdir('data') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'data' userdata Cannot mkdir on an existing directory

PythonManipulating Directories and Files >>> mkdir('country/regions/towns') user

PythonManipulating Directories and Files >>> mkdir('country/regions/towns') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 2] No such file or directory: 'country/regions/towns' user mkdir cannot make nested directories

PythonManipulating Directories and Files >>> mkdir('country/regions/towns') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 2] No such file or directory: 'country/regions/towns' >>> from os import makedirs >>> makedirs('country/regions/towns') user mkdir cannot make nested directories but makedirs can

PythonManipulating Directories and Files >>> mkdir('country/regions/towns') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 2] No such file or directory: 'country/regions/towns' >>> from os import makedirs >>> makedirs('country/regions/towns') country regions towns user mkdir cannot make nested directories but makedirs can

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') country regions towns user

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') country regions user

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') >>> rmdir('country') country regions user

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') >>> rmdir('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country' country regions user rmdir cannot remove nested directories

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') >>> rmdir('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country‘ >>> from os import removedirs >>> removedirs('country') rmdir cannot remove nested directories but removedirs can country regions user

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') >>> rmdir('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country‘ >>> from os import removedirs >>> removedirs('country') rmdir cannot remove nested directories but removedirs can country user

PythonManipulating Directories and Files >>> from os import rmdir >>> rmdir('country/regions/towns') >>> rmdir('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country‘ >>> from os import removedirs >>> removedirs('country') rmdir cannot remove nested directories but removedirs can user

PythonManipulating Directories and Files >>> removedirs('country') 1.txt2.txt country regions user

PythonManipulating Directories and Files >>> removedirs('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country' 1.txt2.txt country regions user removedirs cannot remove directories with files

PythonManipulating Directories and Files >>> removedirs('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country' >>> from shutil import rmtree >>> rmtree('country') 1.txt2.txt country regions user removedirs cannot remove directories with files but rmtree can

PythonManipulating Directories and Files >>> removedirs('country') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists: 'country' >>> from shutil import rmtree >>> rmtree('country') user removedirs cannot remove directories with files but rmtree can

PythonManipulating Directories and Files >>> from os import remove >>> remove('1.txt') 1.txt2.txt datauser remove removes individual files

PythonManipulating Directories and Files >>> from os import remove >>> remove('1.txt') 2.txt datauser remove removes individual files

PythonManipulating Directories and Files >>> from os import rename >>> rename('A', 'B/D') A 1.txt2.txt B user rename renames directories C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A', 'B/D') rename renames directories D 1.txt2.txt B user C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A', 'B/D') >>> rename('B/D', 'C') rename renames directories The directory must not exist D 1.txt2.txt B user C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A', 'B/D') >>> rename('B/D', 'C') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists rename renames directories The directory must not exist D 1.txt2.txt B user C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A', 'B/D') >>> rename('B/D', 'C') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists >>> rename('B/D', 'C/D') rename renames directories The directory must not exist D 1.txt2.txt B user C To keep the same directory name, it must be provided explicitly

PythonManipulating Directories and Files >>> from os import rename >>> rename('A', 'B/D') >>> rename('B/D', 'C') Traceback (most recent call last): File " ", line 1, in ? OSError: [Errno 17] File exists >>> rename('B/D', 'C/D') rename renames directories The directory must not exist D 1.txt2.txt B user C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A/1.txt', 'B/3.txt') A 1.txt2.txt B user rename also renames files C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A/1.txt', 'B/3.txt') A 3.txt2.txt B user rename also renames files C

PythonManipulating Directories and Files >>> from os import rename >>> rename('A/1.txt', 'B/3.txt') >>> rename('B/3.txt', 'C') A 3.txt2.txt B user rename also renames files C A destination file name needn’t be given

PythonManipulating Directories and Files >>> from os import rename >>> rename('A/1.txt', 'B/3.txt') >>> rename('B/3.txt', 'C') A 3.txt2.txt B user rename also renames files C A destination file name needn’t be given as the source file name will be used

PythonManipulating Directories and Files >>> from shutil import move >>> move('A/1.txt', 'B/3.txt') move is like rename but more powerful

PythonManipulating Directories and Files >>> from shutil import move >>> move('A/1.txt', 'B/3.txt') move is like rename but more powerful. It preserves Permission bits, group and owner

PythonManipulating Directories and Files >>> from shutil import move >>> move('A/1.txt', 'B/3.txt') move is like rename but more powerful. It preserves Permission bits, group and owner Last access and modification times

PythonManipulating Directories and Files >>> from shutil import move >>> move('A/1.txt', 'B/3.txt') move is like rename but more powerful. It preserves Permission bits, group and owner Last access and modification times Other flags

PythonManipulating Directories and Files >>> from os import renames >>> renames('A/1.txt', 'B/D/3.txt') renames behaves like both rename makedirs A 1.txt2.txt B user

PythonManipulating Directories and Files >>> from os import renames >>> renames('A/1.txt', 'B/D/3.txt') renames behaves like both rename makedirs It creates any intermediate directories A 3.txt 2.txt B user D

PythonManipulating Directories and Files >>> from shutil import copytree >>> copytree('A', 'B') A 1.txt2.txt user copytree copies directories and files

PythonManipulating Directories and Files >>> from shutil import copytree >>> copytree('A', 'B') A 1.txt2.txt user B 1.txt2.txt copytree copies directories and files recursively It preserves Permission bits, group and owner Last access and modification times Other flags

PythonManipulating Directories and Files >>> from shutil import copyfile >>> copyfile('A/1.txt', 'B/3.txt') A 1.txt2.txt user B copyfile copies files

PythonManipulating Directories and Files >>> from shutil import copyfile >>> copyfile('A/1.txt', 'B/3.txt') A 1.txt2.txt user B 3.txt copyfile copies files

PythonManipulating Directories and Files >>> from shutil import copyfile >>> copyfile('A/1.txt', 'B/3.txt') >>> copyfile('B/3.txt', 'A') A 1.txt2.txt user B 3.txt copyfile copies files

PythonManipulating Directories and Files >>> from shutil import copyfile >>> copyfile('A/1.txt', 'B/3.txt') >>> copyfile('B/3.txt', 'A') Traceback (most recent call last): File " ", line 1, in ? File "/usr/local/lib/python2.4/shutil.py", line 48, in copyfile fdst = open(dst, 'wb') IOError: invalid mode: wb A 1.txt2.txt user B 3.txt copyfile copies files A destination file name must always be given

PythonManipulating Directories and Files >>> from shutil import copy >>> copy('A/1.txt', 'B') A 1.txt2.txt user B copy also copies files

PythonManipulating Directories and Files >>> from shutil import copy >>> copy('A/1.txt', 'B') A 1.txt2.txt user B 1.txt copy also copies files Unlike copyfile, no target file name needs to be given

PythonManipulating Directories and Files >>> from shutil import copy >>> copy('A/1.txt', 'B') A 1.txt2.txt user B 1.txt copy also copies files Unlike copyfile, no target file name needs to be given It also copies existing file permissions

PythonManipulating Directories and Files >>> from shutil import copy >>> copy('A/1.txt', 'B') >>> from shutil import copy2 >>> copy2('A/1.txt', 'B') A 1.txt2.txt user B 1.txt copy also copies files Unlike copyfile, no target file name needs to be given It also copies existing file permissions copy2 also copies files

PythonManipulating Directories and Files >>> from shutil import copy >>> copy('A/1.txt', 'B') >>> from shutil import copy2 >>> copy2('A/1.txt', 'B') A 1.txt2.txt user B 1.txt copy also copies files Unlike copyfile, no target file name needs to be given It also copies existing file permissions copy2 also copies files. It also copies Permission bits, group and owner Last access and modification times Other flags copytree uses copy2

PythonManipulating Directories and Files os Miscellaneous operating system interfaces mkdir Make a directory makedirs Make a directory and any intermediate directories rmdir Remove an empty directory removedirs Remove all empty directories in a path remove Remove a file rename Rename a file renames Rename a file, creating any intermediate directories shutil High-level file operations rmtree Remove a directory and all its contents move Move a file or a directory copytree Copy a directory and all its contents, using copy2. copyfile Copy a file’s contents copy Copy a file preserving the file permissions copy2Copy a file, preserving file permissions, group, owner, last access and modification times and flags

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