Can I be able to read a text file from any text editor (eg Ms word, wordpad) using python. and then return whats inside?

Recommended Answers

All 3 Replies

Yes. You can use open to open a file and return the file handle. Note that some formats (ie, .doc) are not "flat" text files, meaning they may have some binary data, markup, etc.

You'll need to take that into account on a per-file basis.

all I can find in my python books is that I can open a file eg
f = open("filename",'w') and then write to the file using f.write()
and stuff. So how do I open eg a file with file name jag.doc that is stored in my documents folder, how do I specify where the file is located?

So how do I open eg a file with file name jag.doc that is stored in my documents folder, how do I specify where the file is located?

To read a file, you use the 'r' (read) mode instead of 'w' (write).

To specify the file's location you can type the absolute path to the file, ie C:\Documents and Settings\<user_name>\My Documents\jag.doc ; however in PYthon \ is an escape character. In light of this you could either use the forward slash / like linux or use a double backslash \\

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.