Browsing Directories 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

Directory and File Paths Copyright © Software Carpentry and The University of Edinburgh This work is licensed under the Creative Commons Attribution.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Unix system calls (part 1) history and.
Slicing Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Tuples Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Job Control Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Unix (CA263) File System
Copying Web images (the right mouse button is your friend) Skills: Copy a Web image, see the properties of a Web image, use the right mouse button IT concepts:
Copyright © 2014 Dr. James D. Palmer; This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Introduction Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Using Macs and Unix Nancy Griffeth January 6, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis of Complex.
Python programs How can I run a program? Input and output.
Introduction to Python Genome 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Fixtures Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Nanotech Example Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Basic File Input and Output Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Agenda Link of the week Use of Virtual Machine Review week one lab assignment This week’s expected outcomes Review next lab assignments Break Out Problems.
The Secure Shell Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
Pipes and Filters Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Advanced Shell Tricks Copyright © The University of Southampton 2011 This work is licensed under the Creative Commons Attribution License See
Macros Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Libraries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Files and Directories 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"
Interface and Implementation 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
Intro to UNIX Presented by: Student Ambassadors: Lauren Lewis Martin Sung.
Introduction Copyright © Software Carpentry This work is licensed under the Creative Commons Attribution License See
More Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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
Lists Copyright © Software Carpentry 2010 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
CS 245 – Part 1 Using Operating Systems and Networks for Programmers Jiang Guo Dept. of Computer Science California State University Los Angeles.
PROGRAMMING IN PYTHON LETS LEARN SOME CODE TOGETHER!
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
Xi Wang Yang Zhang. 1. Easy to learn 2. Clean and readable codes 3. A lot of useful packages, especially for web scraping and text mining 4. Growing popularity.
Manipulating Directories and Files Copyright © The University of Edinburgh 2011 This work is licensed under the Creative Commons Attribution License See.
Today… Strings: –String Methods Demo. Raising Exceptions. os Module Winter 2016CISC101 - Prof. McLeod1.
Patterns Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Python Aliasing Copyright © Software Carpentry 2010
Getting started with CentOS Linux
Program Design Invasion Percolation: Aliasing
Sets and Dictionaries Examples Copyright © Software Carpentry 2010
Introduction to Python
The Linux Operating System
Sarah Diesburg Operating Systems CS 3430
UNIX & file system cosc513 Deyong Xu ID: Nov
Python Mr. Husch.
Winter 2018 CISC101 12/5/2018 CISC101 Reminders
RegRipper Harlan Carvey.
Version Control Basic Operation Copyright © Software Carpentry 2010
Getting started with CentOS Linux
Version Control Introduction Copyright © Software Carpentry 2010
Python 19 Mr. Husch.
[AttuneII]How to get the screenshot on the Center Module
[AttuneII]How to get the screenshot on the Center Module
CISC101 Reminders Assn 3 sample solution is posted.
Python Lesson’S 1 & 2 Mr. Kalmes.
Version Control Conflict Copyright © Software Carpentry 2010
Program Design Invasion Percolation: The Grid
CS-0110 Introduction to Windows XP
Browsing Directories Using walk
Python 19 Mr. Husch.
Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.
Presentation transcript:

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

Browsing Directories We can use Python to

PythonBrowsing Directories We can use Python to –Save data to files

PythonBrowsing Directories We can use Python to –Save data to files –Read data from files

PythonBrowsing Directories We can use Python to –Save data to files –Read data from files But we might also want to

PythonBrowsing Directories We can use Python to –Save data to files –Read data from files But we might also want to –See what files we have

PythonBrowsing Directories We can use Python to –Save data to files –Read data from files But we might also want to –See what files we have –Delete files

PythonBrowsing Directories We can use Python to –Save data to files –Read data from files But we might also want to –See what files we have –Delete files –Group these into directories

PythonBrowsing Directories We can use Python to –Save data to files –Read data from files But we might also want to –See what files we have –Delete files –Group these into directories –Structure these directories into a tree

PythonBrowsing Directories We could use the shell

PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python

PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python –Shell commands

PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python –Shell commands This is not portable

PythonBrowsing Directories We could use the shell Our program will be a mixture of –Python –Shell commands This is not portable Do it all in Python

PythonBrowsing Directories >>> from os import getcwd Import getcwd from the os module

PythonBrowsing Directories >>> from os import getcwd >>> getcwd()

PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' Current working directory

PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' >>> originaldir = getcwd() Save the current working directory in a variable

PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' >>> originaldir = getcwd() >>> print originaldir Use the variable

PythonBrowsing Directories >>> from os import getcwd >>> getcwd() '/users/vlad' >>> originaldir = getcwd() >>> print originaldir /users/vlad

PythonBrowsing Directories >>> from os import listdir

PythonBrowsing Directories >>> from os import listdir >>> listdir('.') vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc

PythonBrowsing Directories >>> from os import listdir >>> listdir('.') vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc Current working directory

PythonBrowsing Directories >>> from os import listdir >>> listdir('.') ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc

PythonBrowsing Directories >>> from os import listdir >>> listdir('.') ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc

PythonBrowsing Directories >>> listdir('.')

PythonBrowsing Directories >>> listdir(getcwd()) Use the result of getcwd as the input directory to listdir

PythonBrowsing Directories >>> listdir(getcwd()) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music']

PythonBrowsing Directories >>> listdir(originaldir) Use a variable as the input directory to listdir

PythonBrowsing Directories >>> listdir(originaldir) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] Use a variable as the input directory to listdir

PythonBrowsing Directories >>> listdir(originaldir) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] >>> files = listdir(originaldir)

PythonBrowsing Directories >>> listdir(originaldir) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] >>> files = listdir(originaldir) >>> print files

PythonBrowsing Directories >>> listdir(originaldir) ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music'] >>> files = listdir(originaldir) >>> print files ['solar', 'mail', 'pizza.cfg', 'notes.txt', 'swc', 'data', 'papers', 'solar.pdf', 'bin', 'music']

PythonBrowsing Directories >>> for file in files:Remember the colon

PythonBrowsing Directories >>> for file in files:... print file Remember the 4 spaces

PythonBrowsing Directories >>> for file in files:... print file... Remember RETURN to close the loop

PythonBrowsing Directories >>> for file in files:... print file... solar mail pizza.cfg notes.txt swc data papers solar.pdf bin music vlad binmusicmail papers data solar notes.txt pizza.cfg solar.pdf swc

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data')

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd()

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' chdir changes the current working directory

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> listdir(getcwd())

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> listdir(getcwd()) ['morse.txt', 'pdb', 'planets.txt', 'amino_acids.txt', 'elements', 'sunspot.txt']

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> listdir(getcwd()) ['morse.txt', 'pdb', 'planets.txt', 'amino_acids.txt', 'elements', 'sunspot.txt'] >>> chdir(originaldir)

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> listdir(getcwd()) ['morse.txt', 'pdb', 'planets.txt', 'amino_acids.txt', 'elements', 'sunspot.txt'] >>> chdir(originaldir) >>> getcwd()

PythonBrowsing Directories >>> getcwd() '/users/vlad' >>> from os import chdir >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> listdir(getcwd()) ['morse.txt', 'pdb', 'planets.txt', 'amino_acids.txt', 'elements', 'sunspot.txt'] >>> chdir(originaldir) >>> getcwd() '/users/vlad'

PythonBrowsing Directories >>> chdir('data') bindatauserstmp / root imhoteplarryvlad data

PythonBrowsing Directories >>> chdir('data') >>> getcwd() bindatauserstmp / root imhoteplarryvlad data

PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory

PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> CTRL-D bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory

PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> CTRL-D $ pwd bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory

PythonBrowsing Directories >>> chdir('data') >>> getcwd() '/users/vlad/data' >>> CTRL-D $ pwd '/users/vlad' bindatauserstmp / root imhoteplarryvlad data What Python considers to be the current working directory What the shell considers to be the current working directory

PythonBrowsing Directories os Miscellaneous operating system interfaces getcwd Get current working directory listdir List directory contents chdirChange directory

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.