-3

Long story short I want to do a project but don't even know where to start or how to describe it for a google search.

When I run my python file in the Command Prompt I want to be able put a text file next to it that my program will read.
Like: D:> python3 name_of_file.py name_of_text_file.txt

Now when I execute the above I want my python file to somehow see the text file to do something with it (like outputting the contents) but I don't know how to do that. Any suggestions?

Now since I don't know where to start I do not have anything to show on what I have already tried.
Incase it helps: I am using Atom. & The python file and the text file are saved in the same folder.

ianmw7777
  • 9
  • 3

2 Answers2

0

Within a python script you can use the list sys.argv. The first list element (sys.argv[0]) is the name of the python script file (in your case "name_of_file.py"). The rest of the list elements are the provided command line arguments. So sys.argv[1] will contain the first command line argument.

import sys

# Retrieve first command line argument
file_name = sys.argv[1]

# Open text file
with open(file_name) as file:
    # Read out contents of text file
    file_text = file.read()
    
    print(file_text)
Xukrao
  • 6,984
  • 4
  • 25
  • 50
  • Thank you for your detailed explanation on what exactly is happening in the code. That is how people learn and get better. – ianmw7777 Jan 23 '22 at 19:07
-2

You can use argparse to parse command line arguments.

from argparse import ArgumentParser

parser = ArgumentParser(description="Script Name")

parser.add_argument("file_path", help="The path of the text file.", type=str)

args = parser.parse_args()

with open(args.file_path, "r") as test:
    print(test.read())
Didlly
  • 87
  • 7
  • Is there a way where I do not have to specify the text file's path in my code? I would like to specify the text file in question in the command prompt where I am executing the code. – ianmw7777 Jan 23 '22 at 18:52
  • 2
    @ianmw7777 I don't understand what you mean. The file path is never specified in the code. The code I have included reads the file path from the command line arguments. Just run 'python3 name_of_file.py name_of_text_file.txt' – Didlly Jan 23 '22 at 18:53
  • You have "The path of the text file." in the code you included and I don't want to have to specify the whole path of the text file just the name. Is that even possible? – ianmw7777 Jan 23 '22 at 18:56
  • @ianmw7777 If the file is in the same directory as the Python script, it is possible. If the file is in a different directory, it is not possible. Even if you just specify the file's name, you have to specify it's extension (e.g. '.txt'). – Didlly Jan 23 '22 at 18:58
  • Thank you for your help and time Didlly. – ianmw7777 Jan 23 '22 at 19:05
  • 1
    @Didlly The file needs to be in the _invoking user's current working directory._ See [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee Jan 23 '22 at 20:00