Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 10 Online
OpenStudy (anonymous):

how to open and print a txt in python.? code: from sys import argv script, filename = argv txt = open(filename) print "Here's your file %r:" % filename print txt.read() but it does no work..

OpenStudy (harsimran_hs4):

look first of all when you open a file you need to specifiy the mode e.g f = open(filename, mode) modes are 'r+' , 'r' etc. so in your case 'r' mode will be best

OpenStudy (harsimran_hs4):

@camouflage Did this solved your problem ?

OpenStudy (anonymous):

@harsimran_hs4 but how can python find my txt..giving it the path ? or only the file name .

OpenStudy (harsimran_hs4):

if you file and script are in the same folder then filename would do (eg. abc.txt) otherwise you need to specify full path

OpenStudy (harsimran_hs4):

*your file

OpenStudy (harsimran_hs4):

@camouflage it would be better if you show the code

OpenStudy (anonymous):

@harsimran_hs4 my code is on the above ..it tells me that the name of the file is not defined

OpenStudy (harsimran_hs4):

what are you giving for filename in command prompt

OpenStudy (anonymous):

@harsimran_hs4 oh i gives it test1. it tells no such file

OpenStudy (harsimran_hs4):

is your file in the same folder ? if yes: give test1.txt else: give full path

OpenStudy (anonymous):

harsimran_hs4 i try both .but...can u give me ur code piture..thank u ..

OpenStudy (harsimran_hs4):

run using python test.py test.txt

OpenStudy (anonymous):

oh .thank u very much. i knew

OpenStudy (harsimran_hs4):

Welcome!!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!