1

I am very new to Python. So please give specific advice. I am using Python 3.2.2.

I need to read a large file set in my computer. Now I can not even open it. To verify the directory of the file, I used:

>>> import os
>>> os.path.dirname(os.path.realpath('a9000006.txt'))

It gives me the location 'C:\\Python32'

Then I wrote up codes to open it:

>>> file=open('C:\\Python32\a9000006.txt','r')
Traceback (most recent call last):
    File "<pyshell#29>", line 1, in <module>
      file=open('C:\\Python32\a9000006.txt','r')
IOError: [Errno 22] Invalid argument: 'C:\\Python32\x079000006.txt'

Then I tried another one:

>>> file=open('C:\\Python32\\a9000006.txt','r')
Traceback (most recent call last):
    File "<pyshell#33>", line 1, in <module>
      file=open('C:\\Python32\\a9000006.txt','r')
IOError: [Errno 2] No such file or directory: 'C:\\Python32\\a9000006.txt'

Then another one:

>>> file=open(r'C:\Python32\a9000006.txt','r')
Traceback (most recent call last):
    File "<pyshell#35>", line 1, in <module>
      file=open(r'C:\Python32\a9000006.txt','r')
IOError: [Errno 2] No such file or directory: 'C:\\Python32\\a9000006.txt'

The file is saved in the Python folder. But, it is in a folder, so the path is D\Software\Python\Python3.2.2\Part1\Part1awards_1990\awd_1990_00. It is multiple layers of folders.

Also, and anyone share how to read the abstract section of all files in that folder? Thanks.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Q-ximi
  • 941
  • 3
  • 14
  • 21
  • `os.path.dirname()` does **not** search for a file for you. Instead, it tries to determine the directory a given path is in; for relative files that is always the current working directory (as that's what they'd be relative to). – Martijn Pieters Feb 23 '14 at 03:25
  • Use the full path if you need to open a file from a specific directory instead. – Martijn Pieters Feb 23 '14 at 03:32

1 Answers1

0

\a is the ASCII bell character, not a backslash and an a. Use forward slashes instead of backslashes:

open('C:/Python32/a9000006.txt')

and use the actual path to the file instead of C:/Python32/a9000006.txt It's not clear from your question what that path might be; you seem like you might already know the path, but you're misusing realpath in a way that seems like you're trying to use it to search for the file. realpath doesn't do that.

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • Thanks. I just copied the address to my code and it works now! It looks like:f=open('D:/Software/Python/python-3.2.2/Part1\Part1/awards_1990/awd_1990_00/a9000006.txt','r') – Q-ximi Feb 23 '14 at 05:12