Help with reading file in windows.

Bengt Richter bokr at oz.net
Wed May 21 09:39:26 EDT 2003


On Tue, 20 May 2003 01:33:28 +0100, Hyperion <AgentSmith at Matrix..com> wrote:

>Hi all.
>
> I am in the middle of creating or rather trying to create :) a script
>for searching through a log file for key words etc.etc.
>
>However I am having trouble with opening the file and getting it to
>read.
>
>I have tried.
>
>----------------------------------------- 
>asking = raw_input("what is the file? ")
 print 'File entered was %r' % asking  # do this so we know for sure what you entered
>reading_file = open(asking).r
 reading_file = open(asking)  # open(asking,'r') has the equivalent default
              # file(asking) is the preferred spelling, open being an equivalent alias  
>text_infile = reading_file.read()
>reading_file.close()
>print text_infile,
                  ^-- this means don't print a newline or anything more until the next output
                      and when that comes, print a prefixed space unless the first character
                      is a newline.
>-----------------------------------------
>
>to see if it would print the text, so i could then move on to code it
>some more.
>It didnt work.

Unless I missed something, it should have worked. So I would start checking on
whether you entered the file path exactly right. E.g., with raw_input used as above,
don't double your backslashes the way you would in an ordinary string literal. I.e.,
to get C:<one actual backslash character>xxx the literal would be
'C:\\xxx' or r'C:\xxx' and the way to type it into raw_input would be C:\xxx.


>
>I also tried:
>
>-----------------------------------------
>asking = raw_input("what is the file? ")
>reading_file = file(asking).read()
>print reading_file,
>-------------------------------------
Should have been fine, though I don't know why you are using the trailing comma.

>
>
>Each one of these tries has resulted in an io error of the following:
>
>--------------------------------------------------------------------
>Traceback (most recent call last):
>  File
>"C:\Python22\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
>line 301, in RunScript
>    exec codeObject in __main__.__dict__
>  File "C:\Python-scripts\readingscript.py", line 5, in ?
>    reading_file = file(asking).read()
>IOError: [Errno 2] No such file or directory: 'C:\\Program
>Files\\mIRC\\logs\\#politics.EFNet.txt'
>>>> 
>-----------------------------------------------------------------------------
>
>
>
>What I would like to no is what I doing wrong?
>How am I supposed to represent the log files true path?
>I use "C:\Program Files\mIRC\logs\#politics.EFNet.txt"
If that is supposed to be a literal string in a program with the double quotes,
*generally* you need an r for raw prefixed, like
    r"C:\Program Files\mIRC\logs\#politics.EFNet.txt"
but for this particular string you luck out ;-) The problem is that \r and \n and \t and
a few others are seen as control characters. Note:

 >>> list('C:\fool\a\no\ta\ry')
 ['C', ':', '\x0c', 'o', 'o', 'l', '\x07', '\n', 'o', '\t', 'a', '\r', 'y']
 >>> list(r'C:\fool\a\no\ta\ry')
 ['C', ':', '\\', 'f', 'o', 'o', 'l', '\\', 'a', '\\', 'n', 'o', '\\', 't', 'a', '\\', 'r', 'y']

If "C:\Program Files\mIRC\logs\#politics.EFNet.txt" is what you type at the raw_input prompt,
you should leave off the r and the quotes and not escape your backslashes.


>which is the log files path on my system, but it is still giving me a no
>such file or dir, as you can see from the error.
>
>Has anyone got any ideas what I am doing wrong? or rather were I am
>going wrong?

Try checking for the file from a console ("DOS") window. See what you get if you type
    dir "C:\Program Files\mIRC\logs\#politics.EFNet.txt"
(with the quotes, to be safe, though dir should be ok even with the space).

Then try
    more < "C:\Program Files\mIRC\logs\#politics.EFNet.txt"

Possibly something has the file exclusively opened, and the error message is misleading.

If you try stuff interactively, it helps to copy and paste directly from your interactive
screen, so we know what is actually happening. E.g., if you are trying to simulate interactively
what happens in a CGI program or vice versa, there can be environment and permissions issues.
I.e., the CGI may have a different default directory, the server may not allow absolute paths
outside a certain directory tree etc. etc., and it may be running with different environment
variables and as a different user from you. Plus if you don't copy verbatim from your interactive
screen, there's always a chance of a typo (or a missing typo ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list