import os
import shutil
make a copy of 'file.txt' as 'filecopy.txt' (within the same working directory)
shutil.copy('file.txt','filecopy.txt')
copy 'file.txt' into directory 'myBackup/'
shutil.copy('file.txt','myBackup/')
copy (duplicate) complete directories (including containing files)
shutil.copytree('myDir/','myDirCopy/')
move 'file.txt' to directory 'myFiles'
shutil.move('file.txt','myFiles/')
alternatively, os.rename() can also be used
rename file 'file.txt' into 'filecopy.txt'
os.rename('file.txt','filecopy.txt')
create directory, if not already exit
if not os.path.exists('newDirectory'):
os.makedirs('newDirectory')
directory list of files
filenames=os.listdir('myDirectory')
get working directory
print( os.getcwd() )
get full absolute path
os.path.abspath('myDirectory')
/full/path/to/myDirectory/
cd change into a directory
os.chdir('/path/to/new/directory/')
delete file, empty directory, non-empty directory
# remove a file
os.remove('oldfile')
OSError: [Errno 21] Is a directory:
# remove an empty directory
os.rmdir('oldDir/')
OSError: [Errno 39] Directory not empty:
# delete a directory and all included files
shutil.rmtree('oldDir/')
Get filename from path
# split and join a path
path = 'experiment/data/sample.txt'
# get filename
os.path.basename(path)
'sample.txt'
# get directory name
os.path.dirname(path)
'experiment/data'
# join path using system independent path separator
os.path.join('experiment','data','sample.txt')
'experiment/data/sample.txt'
# add ending path separator if not already there
os.path.join('datafolder', '')
'datafolder/'
os.path.join('datafolder/', '')
'datafolder/'
# get system specific path separator
os.path.sep
'/'
# get filename without extension from a path
path='data/set2.sample6.txt'
os.path.splitext(os.path.basename(path))[0] # get filename before last dot
'set2.sample6'
os.path.basename(path).split('.')[0] # get filename before first dot
'set2'
# get last directory-name in path
os.path.basename(os.path.normpath('/my/path/to/foldername/file.txt'))
'file.txt'
os.path.basename(os.path.normpath('/my/path/to/foldername/'))
'foldername'
os.path.basename(os.path.normpath('/my/path/to/foldername'))
'foldername'
Check if file exist
check if exist file / directory
os.path.exists('file.txt')
os.path.isfile('file.txt')
os.path.isdir('myDir/')
check if any .txt file exist
any(f.endswith('.txt') for f in os.listdir('myDirectory'))
True
check if filename ends with '.txt'
f = 'sample.txt'
f.endswith('.txt')
True
Get list of files
list all files of a directory that ends with '.txt' or '.csv'
[f for f in os.listdir('.') if f.endswith(('.txt','.csv'))]
['sample01.txt', 'sample02.txt', 'metadata.csv']
list all files that contain the text-string 'sample'
[f for f in os.listdir('.') if 'sample' in f]
['sample01.txt', 'sample02.txt']
loop over files in a directory
for f in os.listdir('myDirectory'):
print(f)
loop over txt files using wildcard characters'*'
from glob import glob
for f in glob('*.txt'):
print(f)
same by using fnmatch
import os
from fnmatch import fnmatch
for f in os.listdir('.'):
if fnmatch(f, '*.txt'):
print(f)
or as filename list
filenames = [f for f in os.listdir('myDirectory') if fnmatch(f,'*.txt')]
get filesize
import os
os.path.getsize('sample.txt')
351
get modification time
import os
import time
time.ctime(os.path.getmtime('sample.txt'))
'Tue Nov 5 12:39:38 2013'