创建类似于输入子目录的动态文件夹

问题描述

我有大量的数据文件。这些数据文件是按分钟生成的。它们存储在以每天 DDMMYYYY 命名的文件夹中,如图所示:

Folders are organised as

我最近意识到这些测量文件中有一个恒定的偏移量,所以我想将这些文件导入到 Pandas 数据帧中,删除偏移量并将它们存储回具有相同目录树结构的不同目录中。我已尝试使用以下代码从子目录导入文件

     path = r"C:\Users\LABPC\Desktop\Python\Datafiles"
     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='\t',header=None)
             df=pd.DataFrame(Data)
             df= df.apply(pd.to_numeric) 

我从 os.path.basename(path) 获取直接子目录的名称,但它返回 Datafiles 作为目录名称,当我尝试使用代码创建目录名称和写入文件名时:

write_directory = r"C:\Users\LABPC\Desktop\Python Practice\Datafiles_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       

但我收到一个错误 [Errno 2] No such file or directory: 'C:\\Users\\LABPC\\Desktop\\Python Practice\\Data_Files\\DataFiles_Corrected\\000102.lvm.csv'

我想我在这里遗漏了一些东西。有人可以指导我按照上面提到的正确方法创建文件夹吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)