-1

I have a real beginner question. I would like to do the following:

FileName1 = open(r'C:\Users\data.txt')
PointID = '1'

Now, I'd like to create a new variable that is set to be equal to File1, i.e. a concatenation of File and PointID, or something like this if the variables were both strings:

FieldFile = FileName + PointID

Could anyone suggest a way to combine these into one variable called FieldFile?

Much Appreciated.

AF2k15
  • 250
  • 5
  • 19
  • 3
    What exactly are you hoping to achieve? Do you just want to add `1` to the end of the filename you're passing to `open`, or...? – jonrsharpe Mar 28 '16 at 18:21
  • I should have written the code to look like it does now (I edited it). I am trying to pass the contents of "FileName1" to a new variable called FieldFile. I need it to depend on the PointID because I will have FileName1, FileName2, FileName3... and so on. If I use suggestions from @OrangeFlash81 I get what I asked for, which is a string, but what I want is the contents of my original file not as a string. – AF2k15 Mar 28 '16 at 18:51

1 Answers1

1

When you use open, the result isn't a string. To read the contents of the file as a string, call .read on the result of your open call:

File = open(r'C:\Users\data.txt').read()
PointID = '1'

You can now concatenate your two string variables like you've suggested:

FieldFile = File + PointID
Aaron Christiansen
  • 11,584
  • 5
  • 52
  • 78