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"
Answers:
You can strip
your string
s 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.
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"
You can strip
your string
s 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.