2

I am trying to write a Python script in Spyder to deal with several files at the same time.

Actual path is something like:

/TestCondition/TestDate-A1.txt

I want to control the TestCondition and TestDate only at the beginning.

FoldPath = TestCondition
FileName = TestDate

I want to do something like:

dfA1 = pd.read_csv(FoldPath&'/'Filename&'A1.txt'
dfA2 = pd.read_csv(FoldPath&'/'Filename&'A2.txt'
....
dfA12 = pd.read_csv(FoldPath&'/'Filename&'A12.txt'

#Code with Pandas and Numpy...

How do I concatenate the variable names FoldPath and FileName with the string "A1 to A12" specifically to call out a csv file ? I can't find the correct syntax.

Thanks, J-F

Edit Question solved. With the knowledge of "import os" and also "os.path.join", I can now find a bunch of examples to do what I intended to do. I know that this question has been asked several times, but with my limited knowledge of Python, and programming in general, I could not find the correct key words. Anyway, thanks again for your quick answers.

2
  • use os.path.join Commented Jul 31, 2018 at 23:00
  • and the other os.path functions. Commented Jul 31, 2018 at 23:04

2 Answers 2

1

You can use os.path.join and the + to concatenate

import os
filepath = os.path.join(FoldPath, FileName + '-A1.txt')
dfA1 = pd.read_csv(filepath ...
Sign up to request clarification or add additional context in comments.

Comments

1

first..

dfA1 = pd.read_csv("/{}/{}-A1.txt".format(FoldPath, Filename)

but this code not recommanded.

second.. use os.path.join

dfA1 = pd.read_csv(os.path.join(FoldPath, "{}-A1.txt".format(Filename, ))

1 Comment

Hello. I made it work. Thanks. However, I find that the method proposed below with [...] Filename + '-A1'[...] to be a bit more intuitive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.