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 .....

Recommended Answers

All 7 Replies

where is main() called?

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...

python executes all statements without indention when it is run.

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!")

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.