Home > Software design >  How to concatenate strings within df.to_csv file directory
How to concatenate strings within df.to_csv file directory

Time:11-03

Good day,

I'm currently working on the python script that creates a txt with file name C4-4TH-6TH.txt.

I want the file name to be user input (FileName = C4-4TH-6TH). However, I was having trouble concatenating the complete absolute file path using the sign.

Is the a simpler way to concatenate strings with a file path?

Below is my code:

df.to_csv(r'D:\20210719-Loft\B-Structural\ELEMENT DESIGN\Column\CHB-FCD\C4-4TH-6TH.txt',index=None,sep=\t')

CodePudding user response:

path = "../" #you can change any with / symbol.
name= "abc"

you can create a string as

s = path name '.txt'

CodePudding user response:

The os.path package provides useful path manipulation. In your case I think this should work:

from os import path
filename = input("Enter filename:")
base_path = 'D:\20210719-Loft\B-Structural\ELEMENT DESIGN\Column\CHB-FCD\'
complete_path = path.join(base_path, filename)
df.to_csv(complete_path, index=None, sep=\t')
  • Related