Python Programming with Raspberry Pi Zero by Yamanoor Sai & Yamanoor Srihari
Author:Yamanoor, Sai & Yamanoor, Srihari [Yamanoor, Sai]
Language: eng
Format: azw3
Tags: COM041000 - COMPUTERS / Microprocessors, COM051360 - COMPUTERS / Programming Languages / Python, COM067000 - COMPUTERS / Hardware / General
Publisher: Packt Publishing
Published: 2017-05-04T04:00:00+00:00
Challenge to the reader
The examples discussed with the glob module are available for download along with this chapter as glob_example.py. In one of the examples, we discussed listing files of a specific format. How would you go about listing files that are of the following format: filexxxx.*? (Here x represents any number between 0 and 9. * represents any file extension.)
The shutil module
The shutil module (https://docs.python.org/3/library/shutil.html) enables moving and copying files between folders using the move() and copy() methods. In the previous section, we listed all text files within the folder, txt_files. Let's move these files to the current directory (where the code is being executed) using move(), make a copy of these files once again in txt_files and finally remove the text files from the current directory:
import glob
import shutil
import os
if __name__ == "__main__":
# move files to the current directory
for file in glob.glob('txt_files/file1[0-9][0-9].txt'):
shutil.move(file, '.')
# make a copy of files in the folder 'txt_files' and delete them
for file in glob.glob('file1[0-9][0-9].txt'):
shutil.copy(file, 'txt_files')
os.remove(file)
In the preceding example (available for download along with this chapter as shutil_example.py), the files are being moved as well as copied from the origin to the destination by specifying the source and the destination as the first and second arguments respectively.
The files to be moved (or copied) are identified using the glob module. Then, each file is moved or copied using their corresponding methods.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26522)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19007)
The Fifty Shades Trilogy & Grey by E L James(18961)
Shot Through the Heart by Mercy Celeste(18880)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(16985)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16876)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16805)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16699)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16326)
The Subtle Art of Not Giving a F*ck by Mark Manson(14262)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14072)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13421)
Scorched Earth by Nick Kyme(12716)
Drei Generationen auf dem Jakobsweg by Stein Pia(10923)
Suna by Ziefle Pia(10846)
Scythe by Neal Shusterman(10271)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9476)
Successful Proposal Strategies for Small Businesses: Using Knowledge Management ot Win Govenment, Private Sector, and International Contracts 3rd Edition by Robert Frey(9316)
This is Going to Hurt by Adam Kay(9099)
