日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python之工作举例:通过复制NC文件来造数据

發(fā)布時(shí)間:2023/12/18 python 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python之工作举例:通过复制NC文件来造数据 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

?

1 # 通過對(duì)NC文件復(fù)制來造數(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: 無 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ì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的python之工作举例:通过复制NC文件来造数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。