Link to home
Start Free TrialLog in
Avatar of tech
tech

asked on

Accessing folder/file structure in python

Hi,

I have some csv files which are stored in a folder structure like this

Temp/stn1/Data2014.csv
Temp/stn1/Data2015.csv
Temp/stn2/Data2014.csv
Temp/stn2/Data2015.csv
Temp/stn3/Data2014.csv
Temp/stn3/Data2015.csv

I wanted to save them as stn1.csv, stn2.csv, stn3.csv

This is how I m trying but it's not saving files as desired.
dft = []
for root, dirs, files in os.walk("Temp/", topdown=False):
    for stn in dirs:
        for year in ['2014', '2015']:
            df = pd.read_csv("Temp/"+stn+"/Data"+year+".csv")
            dft.append(df)
      dft = pd.DataFrame(dft)
      filename = "stns/"+str(stn)+".csv"
      dft.to_csv(filename, index=False)

Open in new window

Could you please let me know what I am missing here?
Avatar of Norie
Norie

Where is the folder 'stns'?
Avatar of tech

ASKER

stns folder is inside the Temp folder.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial