-3

When I try to read the file using this code , It gives me an error saying Syntax Error.

import os
import sys
def main(argv):
    bada_file = os.path.join(os.path.dirname(__file__),argv[0],argv[1],)
    print(os.path.abspath(__file__))
    print(os.path.abspath(bada_file))  
    data_list = list()
    with open(bada_file, "r+") as read_file:
        # (1, "first test"), (2, "next_line") ...
        for line in enumerate(read_file.readlines()):
            print(line)    
    for data in data_list:
        print(data)

if __name__ == "__main__":
    main(sys.argv[1:])

The file that I am trying to read is ".OPF".

Is anyone have any suggestions please let me know. It will be really helpful. Thanks you.

Pk_P
  • 1
  • 2
  • 5
    Add the full traceback. It says *much* more than just `SyntaxError`. It will help us help you. – Jongware Mar 22 '20 at 13:54
  • I see one problem, but it wouldn't give a syntax error. You probably intend "for i, line in enumerate" – Kenny Ostrom Mar 22 '20 at 14:16
  • 1
    The message *syntax error* is not complaining about the file you are trying to read. It is complaining about your program. And I don't get a syntax error, which means that the code you posted isn't exactly the code that gives the error you report. Look at where the caret in the message is pointing. – BoarGules Mar 22 '20 at 14:47

1 Answers1

0

Assuming you are reading a file in cwd, I am posting 2 solutions.

os.path.dirname(__file__) return empty if you are executing in cwd and if the file is in cwd, then the code will work, but if you are not in CWD, then it may produce error as it won't the get the correct file path.

Solution : os.path.dirname(os.path.abspath(__file__))

Here is solution.

import os
import sys
def main(argv):

    bada_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),argv[0])

    print(os.path.abspath(__file__))
    print(os.path.dirname(os.path.abspath(__file__)))

    data_list = list()
    with open(bada_file, "r+") as read_file:
       # (1, "first test"), (2, "next_line") ...
       for line in enumerate(read_file.readlines()):
           print(line)    
       for data in data_list:
           print(data)

if __name__ == "__main__":
   main(sys.argv[1:])

Another Simpler solution (using os.getcwd())

import os
import sys

def main(argv):

    for file in argv:

        bada_file = os.path.join(os.getcwd(),file)
    
        print(bada_file)

        #print(os.path.abspath(__file__))
        #print(os.path.abspath(bada_file))
    

        with open(bada_file, "r+") as read_file:
            # (1, "first test"), (2, "next_line") ...
            for line in enumerate(read_file.readlines()):
                print(line)    

if __name__ == "__main__":
    main(sys.argv[1:])
Community
  • 1
  • 1
teddcp
  • 1,514
  • 2
  • 11
  • 25
  • Thanks for your code mate. I am still having same Syntax Error! – Pk_P Mar 23 '20 at 00:58
  • @user13096834 what are you trying to do> how are you running the code in cmd prompt? – teddcp Mar 23 '20 at 03:55
  • Your suggestions are nice, but does not address a Syntax Error... which is what the question is essentially about. Please try to answer only well-asked questions – Tomerikoo Mar 23 '20 at 10:49