做了個簡單的桌面整理的程式,實現功能:
輸入資料夾名稱***,自動在桌面上新建乙個資料夾***,然後將桌面上的檔案的副檔名在***裡新建子資料夾。再把相應到的檔案移動到對應的子資料夾下,如doc檔案都存入doc資料夾裡。如果子資料夾裡已經存在重名的檔案,則不移動。
下圖是子資料夾效果。
上程式:
有個小問題是如果桌面上的資料夾名字帶有英文句號,也會認為是檔案轉移到子資料夾下,可以用os.path.isfile(path)來判斷是資料夾還是檔案。import os
import shutil
# 新建資料夾
defcreatenewfolder
(path):if
not os.path.exists(path)
: os.mkdir(path)
defmovefiles
(path_desktop, new_path)
:# 獲取所有path路徑下的檔名
files_list = os.listdir(path_desktop)
for file_name in files_list:
# 分割檔名,返回元組,如('test', '.txt'),資料夾返回如('整理資料夾', '')
file
= os.path.splitext(file_name)
# 判斷是否是帶副檔名的檔案
iffile[1
]!='':
# 提取副檔名,如txt
expanded_name =
file[1
][1:
]for i in expanded_name:
# 子資料夾路徑
subfolderpath = os.path.join(new_path, expanded_name)
# 判斷資料夾是否存在,若不存在則新建
ifnot os.path.exists(subfolderpath)
: os.mkdir(subfolderpath)
# 判斷檔案是否存在,不存在則移動
ifnot os.path.exists(os.path.join(subfolderpath, file_name)):
shutil.move(os.path.join(path_desktop, file_name)
, subfolderpath)
if __name__ ==
'__main__'
:try
: desktoppath = os.path.join(os.path.expanduser(
'~')
,'desktop'
) newfoldername =
input
('請輸入資料夾名:'
) newfolderpath = os.path.join(desktoppath, newfoldername)
createnewfolder(newfolderpath)
('{}資料夾已經生成!'
.format
(newfoldername)
) movefiles(desktoppath, newfolderpath)
('檔案移動完成!'
)except exception as e:
('error'
, e)
簡單桌面搜尋小工具 java
乙個簡單的桌面搜尋小工具 主要功能 功能一 檔案搜尋 1按照檔名搜尋 2按照副檔名搜尋 3按照字尾名搜尋 功能二 文字檢索 主類 public class mainclass 選單 public class menu 以下三個類實現檔案搜尋 檔案搜尋 public class search 最長公共...
python常用小工具
for i in range 32 image tensor2 image tensor1 0,i plt.imshow image tensor2 plt.show 比如 篩選列表中最後字元為 relu6 的元素 x for x in a if x 5 relu6 tmp 是pil影象 impor...
MSDN論壇桌面小工具試用體驗
今天微軟發布了一款名為msdn論壇小幫手的桌面軟體,試用了一下感覺還不錯,原文是這樣介紹的 有了它,您就可以快速訪問msdn中文論壇的豐富資源 無論是快速進入某個特定技術 比如 c 的提問頁面 還是瀏覽閱讀微軟技術支援團隊精心為您收集 翻譯的精華貼 都只需要您在桌面上輕點兩下滑鼠就好啦!更牛的是論壇...