Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet86/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   82   83   84   85   86   87   88   89   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Further considerations

File operations with the standard library

Python  provides  a  library,  or  module,  called  ‘os’  and  a  sub-module  called  ‘os.path’  that

together provide a lot of functionality which is useful when dealing with files. These two

modules provide functionality to easily navigate around the file system.

Here  is  a  list  of  some  of  the  more  useful  functions.  For  more  details  and  fuller

descriptions, see the Appendices at the end of this book or documentation on the Python

website

http://www.python.org

.

os:


chdir(path) change the current working directory to be path

getcwd() return the current working directory

listdir(path) returns a list of files/directories in the directory

path


mkdir(path) create the directory path

rmdir(path) remove the directory path

remove(path) remove the file path

rename(src, dst) move the file/directory from src to dst

os.path:

exists(path) returns whether path exists

isfile(path) returns whether path is a “regular” file (as

opposed to a directory)

isdir(path) returns whether path is a directory

islink(path) returns whether path is a symbolic link

join(*paths) joins the paths together into one long path

dirname(path) returns directory containing the path

basename(path) returns the path minus the dirname(path) in front



split(path) returns (dirname(path), basename(path))

One  reason  the  path.join  function  is  provided  is  to  abstract  away  details  about  the

operating system, such as whether the directory separator is ‘/’ (Unix, Linux and OS X) or

‘\’ (Windows). For example,

os.path.join('home', 'test', 'mydoc.txt')

returns ‘home/test/mydoc.txt’ on Unix and ‘home\test\mydoc.txt’ on Windows.

Suppose  you  want  to  find  all  files  ending  with  a  specified  suffix  (e.g.  ‘.txt’)  in  a

directory,  and  recursively  include  all  sub-directories.  One  way  to  do  this  using  the

functions listed above in Python is:

import os

def findFiles(directory, suffix):

files = []

dirfiles = os.listdir(directory)

for dirfile in dirfiles:

fullfile = os.path.join(directory, dirfile)

if os.path.isdir(fullfile):

# fullfile is a directory, so recurse into that

files.extend(findFiles(fullfile, suffix))

elif dirfile.endswith(suffix):

# fullfile is a normal file, and with correct suffix

files.append(fullfile)

return files

Note that listdir(directory) returns only the simple, base name of each file, i.e. without

the  names  of  the  directories  in  which  it  is  located,  so  os.path.join()  is  used  to  create  the

full file/directory name. Also, make sure you notice how this function calls itself, to add

more files for each sub-directory.

Suppose  instead  you  wanted  to  remove  all  files  ending  with  a  specific  suffix  in  some

directory, and recursively including all sub-directories. The code would be very similar:

import os

def removeFiles(directory, suffix):

dirfiles = os.listdir(directory)

for dirfile in dirfiles:

fullfile = os.path.join(directory, dirfile)

if os.path.isdir(fullfile):

# fullfile is a directory, so recurse into that

removeFiles(fullfile, suffix)




elif dirfile.endswith(suffix):

# fullfile is a normal file, and with correct suffix

os.remove(fullfile)


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   82   83   84   85   86   87   88   89   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish