file handling in python

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 2
Reputation: sallas07 is an unknown quantity at this point 
Solved Threads: 0
sallas07 sallas07 is offline Offline
Newbie Poster

file handling in python

 
0
  #1
Aug 20th, 2009
Can I be able to read a text file from any text editor (eg Ms word, wordpad) using python. and then return whats inside?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: file handling in python

 
0
  #2
Aug 20th, 2009
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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: sallas07 is an unknown quantity at this point 
Solved Threads: 0
sallas07 sallas07 is offline Offline
Newbie Poster

Re: file handling in python

 
0
  #3
Aug 20th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: file handling in python

 
0
  #4
Aug 21st, 2009
Originally Posted by sallas07 View Post
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 \\
Last edited by jlm699; Aug 21st, 2009 at 2:47 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC