Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

python - Creating the dynamic folders similar as input subdirectories

I have a large amount of data files. These data files are generated on a per minute basis. These are stored in folders named for each day as DDMMYYYY as shown in the image: Folders are organised as.

I recently realized that there is a constant offset in these measured files, so I want to import the files into a pandas dataframe, remove the offset and store them back on a different directory with the same directory tree structure. I have tried the following code to import files from the subdirectories:

     path = r"C:UsersLABPCDesktopPythonDatafiles"
     read_files = glob.glob(os.path.join(path,"*.lvm"))


    for (root, dirs, files) in os.walk(path):
        for filenames in files:
             Data = pd.read_csv(os.path.join(root, filenames), encoding='utf-8', sep='', header=None)
             df=pd.DataFrame(Data)
             df= df.apply(pd.to_numeric) 

I am taking the name of immediate subdirectory from os.path.basename(path) but it returns the Datafiles as the directory name and when I am trying to create the directory names and write filenames using the code:

write_directory = r"C:UsersLABPCDesktopPython PracticeDatafiles_Corrected"
        
      try:
            os.makedirs(write_directory, exist_ok=True)
            output_file = os.path.join(write_directory, os.path.basename(path), filenames + '.csv')
            df1.to_csv(output_file, index=False)
      except FileExistsError:
            # directory already exists
            pass       

But I get an error [Errno 2] No such file or directory: 'C:\Users\LABPC\Desktop\Python Practice\Data_Files\DataFiles_Corrected\000102.lvm.csv'

I think I am missing something around here. Can someone direct me towards the right way to create folders as I mentioned above?

question from:https://stackoverflow.com/questions/65883052/creating-the-dynamic-folders-similar-as-input-subdirectories

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...