i have a very simple program which takes in file name as argument and print the content of the file , but for some reason it isnt working

here is the code

import sys ,os

def Cat(filename):
	f = open(filename)
	text = f.read()
	print '----', filename
	print text
	
	

def main():
	args = sys.argv[1:]
	for arg in args:
		Cat(arg)

i would be glad , if u could help me out .....

Please not use tabs.

import sys, os

def cat(filename):
    f = open(filename)
    text = f.read()
    print '----', filename
    print text

args = sys.argv[1:]
for arg in args:
    cat(arg)

shouldnt i put the code from 9-11 in main() since i want to save it in a py file and run it from a file instead of the interpreter directly...

huh.....? i am not asking about indentation.... since i wanted to run it as a single file . should i put those 9-11 lines in main() and then run or how should i go about doing that ?

Proove the program if you do not trust me, it works:

Microsoft Windows XP [versio 5.1.2600]
(C) Copyright 1985 - 2001 Microsoft Corp.

D:\>cd test

D:\test>printout.py printout.py
---- printout.py
import sys, os

def cat(filename):
    f = open(filename)
    text = f.read()
    print '----', filename
    print text

args = sys.argv[1:]
for arg in args:
    cat(arg)


D:\test>

If you do not call functions they are not executed. def statements are executed in your program as they are in highest level, but you have no function calls in highest level.

Here a "Hello, world" style program in Python 2.6:

print "Hello, I am Python!"

for Python 3

print("Hello, I am Python!")

Edited 6 Years Ago by pyTony: n/a

ohh.. never knew that .. kudos to you .
well i used to always put the function call in the main programs and then execute ..then will this work

def main():
args = sys.argv[1:]
for arg in args:
cat(arg)

This article has been dead for over six months. Start a new discussion instead.