1

i know that the answer is simple and out there somewhere but i cannot seem to find it. As my title suggests, I am trying to write a pandas DF with a dynamic filename in .csv format to an output directory given a path. Here below is my error code below. Thank you for any suggestions and I'm open to doing this a different or pythonic way if possible.

runfile('C:/Users/U321103/.spyder-py3/Read_VORTEX_test_files.py', wdir='C:/Users/U321103/.spyder- 
py3')
klondikeii.Vortex_WIND.csv
Traceback (most recent call last):
File "C:\Users\U321103\.spyder-py3\Read_VORTEX_test_files.py", line 29, in <module>
c.to_csv(Path(p + filename ), index=False)
TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'

My code looks like this:

from sys import exit
import pandas as pd
from pathlib import Path

# Create a dataframe from csv
df = pd.read_csv("\\\porfiler03\\gtdshare\\VORTEX\\VALIDATION\\vortex_links.txt", delimiter=',')
# User list comprehension to create a list of lists from Dataframe rows
list_of_rows = [list(row) for row in df.values]
# Print list of lists i.e. rows
#print(list_of_rows)

var = df.variable.to_frame() #extract wind or power from df 'variable' column
#Find farm_data = farm_vortex cases
#for k in range(0,len(df)):
for k in range(0,1):
  if (( df.farm_data[k] == df.farm_vortex[k]) and var.variable[k] == 'wind'):
    #print('chinook = vortex data')
    c = pd.read_csv(df.link[k])#dataframe with vortex wind data
    filename = df.farm_data[k] + '.' + 'Vortex_WIND' + '.csv'
    print(filename)
    #save the data to a csv_file.
    p = Path('///porfiler03//gtdshare//')
    c.to_csv(Path(p + filename ), index=False)
exit()
1
  • c.to_csv(p.joinpath(filename), index=False) Commented May 29, 2020 at 21:42

1 Answer 1

4

Remove Path() from '///porfiler03//gtdshare//'.

In p + filename you are trying to concatenate to create the full path. You can do so for two strings but not for a string and a path, and this is what the error is telling you. Try:

p = '///porfiler03//gtdshare//'
c.to_csv(Path(p + filename ), index=False)
Sign up to request clarification or add additional context in comments.

Comments

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.