Python Programming for Biology: Bioinformatics and Beyond


Working with variable file names



Download 7,75 Mb.
Pdf ko'rish
bet76/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   72   73   74   75   76   77   78   79   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Working with variable file names

When writing a program we often want it to be able work with different files on different

occasions, each with a different name or file path. Naturally it is possible to sidestep this

issue  by  simply  writing  file  paths  into  the  program  code  and  change  the  program

whenever it needs to work on a different file. However, it is often far easier to accept the

name of the file (or files) as variable arguments when we run the Python program. To do

this we could either use a graphical interface (see

Chapter 26

)  to  select  files  using  a  file

browser, or if we are running a program using the python command we can put the name

of the file argument after the name of the Python script. So, for example, here we refer to a

file  called  ‘inputFile.txt’  in  a  directory  called  ‘data/’  which  we  include  after  the  script

name when running the program:

> python programFile.py data/inputFile.txt

Note  that  the  directory  separator  ‘/’  would  be  a  backslash  ‘\’  on  Windows  systems.

Inside the programFile.py script to access the name of the file from the command line we

simply  need  to  import  sys.argv.  This  is  a  list  containing  all  of  the  text  fields  that  were

filled in on the command line, starting with the name of the Python script itself, when the

script was run:

import sys

pyScriptName = sys.argv[0] # 'programFile.py'

dataFileName = sys.argv[1] # 'data/inputFile.txt'




workFunction(dataFileName) # Use the file name for something…

Thus  by  accessing  sys.argv[1],  and  higher  numeric  indices  for  further  files,  we  can

define file paths (or any other parameters we want to pass to the script) in a dynamic way

when we run the program.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   72   73   74   75   76   77   78   79   ...   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