new line (n) is appending in between file path strings while I trying to read the file path location through argparse

Question:

I tried to read the file through a command line argument from argparse library. But n(new line) is appending in between two paths(folder path and file path) of string while reading from command line arguments. Here is my code

import argparse
parser = argparse.ArgumentParser(description='modelling') #Description 
parser.add_argument('-f', '--filename', type=str, metavar='', required=True, help='data file name') # file reading 
parser.add_argument('-d', '--foldername', type=str, metavar='', required=True, help='data folder path') # Folder

args = parser.parse_args() # reading the arguments

if __name__ == '__main__':
    folder_path = args.foldername
    file_path = args.filename
    print("File path: {0}/{1}".format(folder_path, file_path))

python test.py -f 'something.txt' -d '/home/Documents/data/processed' 

Output:

File Location:/home/Documents/data/processed 
/something.txt

But I want to get the output combined path like this: "/home/Documents/data/processed/something.txt"

Asked By: rk___

||

Answers:

You can strip your strings to fix this. Example:

folder_path = args.foldername.strip()

Depending on the IDE and console you’re using, you may not get away with inserting an extra n in input calls.

Answered By: JustLearning
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.