newbie question; output from simple print statement

E

epost2

Can someone explain to me the output of this simple script? I wonder
why ['test1.txt'] is printed before "files in c:\", and also why None
shows up?

________________
in file test.py:

def main():

print "files in c:\ :%s" % ListFiles("c:\")

def ListFiles(path):
for root,dirs,files in os.walk(path):
print files

if __name__ == "__main__":
main():

________________
output:

C:\> python test.py
['test1.txt']
files in c:\ :None
 
P

Paul Watson

Can someone explain to me the output of this simple script? I wonder
why ['test1.txt'] is printed before "files in c:\", and also why None
shows up?

________________
in file test.py:

def main():

print "files in c:\ :%s" % ListFiles("c:\")

def ListFiles(path):
for root,dirs,files in os.walk(path):
print files

if __name__ == "__main__":
main():

________________
output:

C:\> python test.py
['test1.txt']
files in c:\ :None

The function ListFiles() is being called first. This produces the
result that will be used to replace the %s of the print statement. In
this case, the return value is None, so nothing is printed.

How to Think Like a Computer Scientist
http://www.ibiblio.org.obp/thinkCSpy/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top