python之工作举例:通过复制NC文件来造数据
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                python之工作举例:通过复制NC文件来造数据
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                ?
?
1 # 通過(guò)對(duì)NC文件復(fù)制來(lái)造數(shù)據(jù) 2 import os, shutil 3 4 # 遍歷的根目錄 5 root_dir = "D:\\test_data\\DISASTER\\" 6 # 獲取NC文件的時(shí)間 7 time_source = '20161228080000' 8 # 生成NC文件的時(shí)間 9 time_new = '20181228080000' 10 11 12 def get_dir_path(dir_name, time_str): 13 ''' 14 組裝目錄結(jié)構(gòu) 15 :param dir_name:文件名 16 :param time_str:時(shí)間字符串,如“20161228080000” 17 :return:目錄路徑 18 ''' 19 dir_path = root_dir + dir_name + '\\' + time_str[0:4] + '\\' + time_str[0:6] + '\\' + time_str[0:8] + '\\' 20 return dir_path 21 22 23 def get_new_file_name(source_file_name, time_source, time_new): 24 ''' 25 根據(jù)源文件和時(shí)間生成新的文件名稱 26 :param source_file_name:源文件名 27 :param time_source:源文件時(shí)間 28 :param time_new:新文件時(shí)間 29 :return: 新的文件名 30 ''' 31 list_pices = source_file_name.split('_') 32 # print(list_pices) 33 new_file_name = list_pices[0] 34 for s in list_pices[1:]: 35 if s == time_source: 36 # print(s) 37 new_file_name += '_' + time_new 38 else: 39 new_file_name += '_' + str(s) 40 print("源文件名:", source_file_name) 41 print("新文件名:", new_file_name) 42 return new_file_name 43 44 45 def copy_file(source_file, new_file_name, new_dir): 46 ''' 47 拷貝文件,并檢查文件是否存在 48 :param source_file: 原文件完整路徑包含目錄路徑和文件名 49 :param new_file_name: 新文件名稱 50 :param new_dir: 新文件目錄路徑 51 :return: 無(wú) 52 ''' 53 if os.path.exists(new_dir): 54 print("目標(biāo)目錄已存在:", new_dir) 55 else: 56 print('目標(biāo)目錄新建成功!', new_dir) 57 os.makedirs(new_dir) # 創(chuàng)建多級(jí)目錄 58 # 復(fù)制文件 59 new_whole_file = new_dir + new_file_name 60 shutil.copy(source_file, new_whole_file) 61 if os.path.exists(new_whole_file): 62 print("文件復(fù)制成功!", new_whole_file) 63 else: 64 print("文件復(fù)制失敗!", new_whole_file) 65 66 67 def find_and_copy_nc(root_dir, time_source, time_new): 68 ''' 69 遍歷獲取需要拷貝的原NC文件 70 拷貝到新目錄下 71 :param root_dir: 文件根目錄 72 :param time_source: 源文件時(shí)間 73 :param time_new: 目標(biāo)文件時(shí)間 74 ''' 75 # 遍歷根目錄,獲取天氣現(xiàn)象文件夾列表 76 dir_list = os.listdir(root_dir) 77 for dir in dir_list: 78 '''遍歷各天氣現(xiàn)象要素目錄''' 79 print('#' * 25) 80 print(dir) 81 # 組裝源NC文件父目錄路徑 82 parent_dir = get_dir_path(dir, time_source) 83 print("源目錄路徑:", parent_dir) 84 new_dir = get_dir_path(dir, time_new) 85 print("目標(biāo)目錄路徑:", new_dir) 86 try: 87 ''' 88 獲取NC文件目錄下的文件列表 89 目錄不存在就退出循環(huán) 90 ''' 91 file_list = os.listdir(parent_dir) 92 except: 93 print("源目錄不存在:", parent_dir) 94 continue 95 96 for source_file_name in file_list: 97 '''遍歷NC文件列表''' 98 if source_file_name.count(time_source) > 0: 99 print('-' * 20) 100 # print("源文件名:", source_file_name) 101 new_file_name = get_new_file_name(source_file_name, time_source, time_new) 102 # print("目標(biāo)文件名:", new_file_name) 103 copy_file(parent_dir + source_file_name, new_file_name, new_dir) 104 105 106 find_and_copy_nc(root_dir, time_source, time_new)?
轉(zhuǎn)載于:https://www.cnblogs.com/gongxr/p/7355401.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python之工作举例:通过复制NC文件来造数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: GDB 修改当前判断函数的返回值(即修改
- 下一篇: 数据结构 线性链表栈
