Append Word To Input File Name For Output File Name
I am trying to read in some data, process the data, and write the results to a CSV saved with the original filename + the word 'folded'. I'm using sys.argv to pass the input filen
Solution 1:
It looks like you are already specifying filename.csv
in your command line argument so you want to replace that with your special extension to do that you could:
file_name = sys.argv[1].replace('.csv', '_folded.csv')
Solution 2:
Split the file extension and only use the prefix:
orginal_name = sys.argv[1]
prefix = orginal_name.rsplit('.')[0]
fileout = prefix + '_folded.csv'
or just pass the out
file name from terminal (which I prefer), for example:
python program.py file.csv file_folded.csv
Then in code just use sys.argv[2]
for out
file name.
Solution 3:
You could slice
the .csv off of sys.argv[1]:
fileout = sys.argv[1][:-3] +'_folded.csv'
Solution 4:
To not assume the .csv
extension, use os.path.splitext
:
name, ext = os.path.splitext(sys.argv[1])
file_name = name + '_folded' + ext
Post a Comment for "Append Word To Input File Name For Output File Name"