0

I am trying to read a file in the directory ./resources/input_file.utf8. However, when I compile the following code from the terminal with the command:

 python namefile.py input 

this error appears:

[Errno 2] No such file or directory: 'input' 

This is my code:

from argparse import ArgumentParser def parse_args(): parser = ArgumentParser() parser.add_argument("input_path", help="./resources/input_file.utf8") return parser.parse_args() def foo(input_path): file_input = open(input_path, "r", encoding='utf-8-sig') for line in file_input: [...] if __name__ == '__main__': args = parse_args() predict(args.input_path) 

One of the specification I have to respect is to not to put hard paths directly in the foo function, but only in the parser.add_argument() function.

How can I fix it?

8
  • 3
    Did you try python namefile.py resources/input_file.utf8? Commented Apr 12, 2019 at 15:09
  • The path will be relative to the directory in which your terminal is. Make sure to provide a valid path from there. Commented Apr 12, 2019 at 15:13
  • Sounds like you don't have a file 'input' in your directory Commented Apr 12, 2019 at 15:15
  • @DroidX86 It works! It is the only way to lunch this program? I mean I have always specify the directory before compile the file? The help in the ArgumentParser doesn't specify the directory yet? Thanks Commented Apr 12, 2019 at 15:15
  • 1
    @Pierfrancesco you have to tell the program where to find the file you're trying to use. You can do this either with a relative path or an absolute path. Absoute paths start from root /. Relative paths starts from either the current directory or a well-known location (like ~ - your home directory) Commented Apr 12, 2019 at 15:26

1 Answer 1

1

If you are running the command python namefile.py input, make sure that:

1) Files - You have both your input file and the python script in the same folder.

2) Location - The working directory in you terminal is the one that contains the files (use pwd to check).

3) File name - Your filename is exactly input and not input.txt or input.utf8.


If your file is in the path that you have provided in your example, then you would need to call the script with that path.

Example:

python namefile.py "./resources/input_file.utf8"

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.