0

Please help me out with this code as I am a beginner in python struggling . There needs to be one xyz file for every data file.The output format should be as follows : [ID]> [Year]>[Year-Month]>[Sub-ID]>[xyz.txt] The station ID for data is IN-023C Year to be determined based on the timestamp of the file Year-Month needs to be determined based on the timestamp of the file Substation-ID depends on the substation read (if in file I1 then it should be placed in folder Ipl1 if I2 then folder IPL2)xyz.txt has the same name as the file.txt same file name :[IN-023C]-I1-2018-12-01.txt code I have written :

import os 
from shutil import copyfile
         folder_path = '*C:\Users\P&K\Desktop\Assign\*'
         os.chdir(folder_path)
         for file in os.listdir(folder_path):
          stationid = file[1:9]
          year = file[14:17]
          month = file[14:20]
          substation = file[11:12]
          if file == 'I1':
          print ('Inverter1')
          break
          else:
          print('Inverter2')
          final_path = folder_path + '/' + stationid + '>' + year + '>'+'month'+'>'+'substation'+ '/' + 
          file + .txt'
          copyfile(file, final_path)
  • Does this answer your question? [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – Joe Apr 10 '20 at 11:32
  • 1
    https://stackoverflow.com/search?q=Python+Iterate+file+directory – Joe Apr 10 '20 at 11:33
  • take a look at this site[ https://www.w3schools.com/python/python_file_handling.asp ] – Nafiseh Daneshian Apr 10 '20 at 12:48
  • So , the code which I wrote is a wrong way - to get the year and month from the file, how do I do that – Pavani Nayak Apr 11 '20 at 19:36

0 Answers0