File operations‎ > ‎

File commands

import os
import shutil


copy 'file.txt' to 'filecopy.txt'
shutil.copy('file.txt','filecopy.txt')

copy 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() )


cd  change into a directory
os.chdir('/path/to/new/directory/')

delete file, empty directory, non-empty directory
os.remove('oldfile') # remove a file
OSError: [Errno 21] Is a directory:
os.rmdir('oldDir/')  # remove an empty directory
OSError: [Errno 39] Directory not empty:
shutil.rmtree('oldDir/') # delete a directory and all included files

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'


Get list of files


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


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')
441

get modification time
>>> import os
>>> import time
>>> time.ctime(os.path.getmtime('sample.txt'))
'Tue Nov  5 12:39:38 2013'