檔案管理 實現檔案的唯一性(部分功能)

2021-10-07 19:25:34 字數 3029 閱讀 7057

# coding:utf-8

import os

import hashlib

import time

"""日期:2023年6月29日

版本:1.0

功能描述:

用於管理雷射下料工位的dxf檔案,也可以用於其他檔案的處理,未加篩選條件,(程式原本屬於dxf雷射下料管理程式的部分**)

1.批量更新

更新所有的dxf檔案,使其所有的dxf檔案都保持最新;

實現乙個更新,全部最新。

2.批量刪除

一次刪除所有的舊dxf檔案。

"""def

all_dxf_dict

(file_dir=

"./"):

""" 功能描述:

掃瞄指定路徑下的資料夾,列出所有的檔案

依賴:import os

:param: str 掃瞄路徑

:return: dict 掃瞄結果

-掃瞄的結果

"""dxf_dict =

# 掃瞄資料夾,列出所需要的檔案

for root, dirs, files in os.walk(file_dir)

:# print(root,'-' ,dirs,'-' ,files) # 當前目錄路徑

# print() # 當前路徑下所有子目錄

# print() # 當前路徑下所有非目錄子檔案

# 當前的檔名稱:建立或新增

for j in files:

if j not

in dxf_dict:

dxf_dict[j]

=[root]

else

: dxf_dict[j]

print

(dxf_dict)

return dxf_dict

defhash_contrast

(file_a=

'./6666.png'

, file_b=

'./新建資料夾 (2)/666.png'):

""" 功能描述:

分辨兩個檔案是否為同一檔案

通過雜湊值得對比實現識別

:依賴: import hashlib

:param: str,str 兩個檔案的路徑

:return: bool 兩個檔案是否是一樣的

-對比結果

"""defget_file_md5

(f):

m = hashlib.md5(

)while

true

:# 如果不用二進位制開啟檔案,則需要先編碼

# data = f.read(1024).encode('utf-8')

data = f.read(

1024

)# 將檔案分塊讀取

ifnot data:

break

m.update(data)

return m.hexdigest(

)with

open

(file_a,

'rb'

)as f_a,

open

(file_b,

'rb'

)as f_b:

a = get_file_md5(f_a)

b = get_file_md5(f_b)

print

(a, b, a == b)

return a == b

deffile_create_time_contrast

(file_name=

'666.png'

, path=

['./'

,'./新建資料夾 (2)'])

:"""

功能描述:

得到多個路徑的檔案中的最後儲存的那乙個

:依賴:

import os

import time

:param: str,list 檔案的名稱 檔案的路徑的列表

:return: str 最新的那個檔案的檔案路徑

"""newly =

none

path_newly =

''for i in path:

if i[-1

]!='\\'

and i[-1

]!='/':

i = i +

'/' m_time = time.strftime(

'%y-%m-%d %h:%m:%s'

, time.localtime(os.stat(i + file_name)

.st_ctime)

)# 檔案的建立時間

if newly:

if newly < m_time:

newly = m_time

path_newly = i

else

: newly = m_time

path_newly = i

# mtime = time.strftime('%y-%m-%d %h:%m:%s', time.localtime(os.stat("./666.png").st_mtime)) # 檔案的修改時間

# ctime = time.strftime('%y-%m-%d %h:%m:%s', time.localtime(os.stat("./666.png").st_ctime))# 檔案的建立時間

print

('比較新的檔案的修改時間'

,newly,

"比較新的檔案的路徑"

,path_newly+file_name)

return path_newly+file_name

if __name__ ==

'__main__'

:# all_dxf_dict()

file_create_time_contrast(

)

實現UniqueAttribute唯一性約束

using system using system.componentmodel.dataannotations using system.data.entity namespace zwj.tems.base public override boolean isvalid object value...

實現UniqueAttribute唯一性約束

using system using system.componentmodel.dataannotations using system.data.entity namespace zwj.tems.base public override boolean isvalid object value...

程式的唯一性

試過各種方法,下面這個相對比較好 在program.cs中,新增如下,紅色字部分要改掉 usingsystem.diagnostics 新增 namespace programunique static class program 應用程式的主入口點。stathread static void ma...