Python Programming for Biology: Bioinformatics and Beyond



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

Writing files

As with reading a file, in order to write a file in a Python program, you need to specify the

path  to  the  file  and  use  the  ‘open’  function  to  define  an  object  and  get  access  to  it.  The

difference  is  that  the  file  mode  here  is  ‘w’,  which  stands  for  ‘writing’,  instead  of  ‘r’  or

‘rU’:

path = 'output.txt'



fileObj = open(path, "w")

This creates an (initially) empty file. Note that if a file with that path already exists then

it will first be deleted before the new file is created, so this can be a dangerous operation.

You  can  check  whether  the  file  exists  using  os.path.exists(path)  and  then  ask  the  user

whether they really want to overwrite what is there. If you want to append to the end of a

file, rather than create a new one, then you can use the append mode:

fileObj = open(path, "a")

The  most  commonly  used  writing  functions  are  write()  and  writelines().  There  is  no

function  corresponding  to  readline  because  write  serves  that  purpose.  Thus  write(data)

writes out the string data and it doesn’t care whether or not there is a newline character (or

carriage return) in the string. The writelines(lines) function writes out a list of strings, and

again  is  indifferent  as  to  whether  or  not  the  strings  have  a  newline  character.  Given  the

fact  that  these  functions  are  somewhat  free-form  with  regard  to  what  is  written  out  and

how the lines of the file are defined (i.e. with ‘\n’ or ‘\r\n’), you need to be sure that the

end-of-line characters are added in the right way.

As  with  reading,  when  you  are  done  with  a  file-handling  object  for  writing  then  you

should generally close it explicitly:

fileObj.close()




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   79   80   81   82   83   84   85   86   ...   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