TIME NOW
World current time now,
CALENDAR
Calendar monthly, yearly
login CONVERT LENGTH
login CONVERT TEMPERATURE
login DICTIONARIES, LISTS
login SCIENCE EDUCATION RELIGION
login WORK CALCULATOR
login CALCULATE LIFE

Python 3 programming language, learn python, tutorial

Previous articlePage bottomNext article  ALL TOPICS

Python 3. Module shutil

 Copy:
import shutil

shutil.copyfile('data.db', 'archive.db')
shutil.move('/build/executables', 'installdir')

Copy files from one forder to another:
import shutil
import os

flrd="c:/xampp/htdocs"

source = os.listdir(flrd)
destination = "c:/xampp/old"

os.chdir(flrd)

try:
  os.mkdir(destination)
except FileExistsError:
  pass
 
for files in source:

    if files.endswith(".txt"):
        print(files)    
        shutil.copy(files,destination)

Move files:
import shutil
import os

flrd="c:/xampp/htdocs"

source = os.listdir(flrd)
destination = "c:/xampp/old"

os.chdir(flrd)

try:
  os.mkdir(destination)
except FileExistsError:
  pass
 
for files in source:

    if files.endswith(".txt"):
        print(files)
        
        try:    
            shutil.move(files,destination)
        except shutil.Error:
            print (" ----- ----- ----- file exists")

Additional:
shutil.copy2(src, dst)
Similar to shutil.copy(), but metadata is copied as well – in fact, this is just shutil.copy() followed by copystat(). This is similar to the Unix command cp -p.
 
shutil.copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

shutil.rmtree(path, ignore_errors=False, onerror=None)

Copy and remove folders:
import shutil
import os

SOURCE = "c:/xampp/htdocs"
BACKUP = "c:/xampp/backup"

# create a backup directory
shutil.copytree(SOURCE, BACKUP)

print (os.listdir(BACKUP))

# remove it
shutil.rmtree(BACKUP)

try:
  print (os.listdir(BACKUP))
except FileNotFoundError:
  print ("There is no such folder")
  
Previous articlePage topNext article  ALL TOPICS



 Use username: Guest, Anonymous, Programmer






QUOTES:
The little trouble in the world that is not due to love is due to friendship.
Edgar Watson Howe
I'm sure glad my ancestors did not invent the flush toilet. Then my name would be crap. Poor Thomas Crapper.
John
Words lead to deeds.... They prepare the soul, make it ready, and move it to tenderness.
Anonymous