當前位置:首頁 » 編程語言 » pythondest

pythondest

發布時間: 2023-10-13 22:16:31

python把一個文件夾下的所有東西復制到另一個文件夾下

fromshutilimport
importos
importre
dest_dir=raw_input('Pleaseenterdestinationpath:(splitpathwith"/")')
source_dir=raw_input('Pleaseentersourcepath:(splitpathwith"/")')

ifnotdest_dir.endswith('/'):
dest_dir+='/'
ifnotsource_dir.endswith('/'):
source_dir+='/'
ifos.path.isdir(dest_dir)andos.path.isdir(source_dir):
forroot,dirs,filesinos.walk(source_dir):
foriinxrange(0,files.__len__()):
sf=os.path.join(root,files[i])
dst=re.sub('([A-Za-z]:/.*?)/',dest_dir,root)
ifnotos.path.exists(dst):
os.makedirs(dst)
(sf,dst)
print'Done!'

else:
raiseException('Wrongpathentered!')

raw_input()

Ⅱ python包管理工具pip download詳解

pip download的用法與install相似,只不過download只下載安裝包並不安裝到系統內,這里我們只講述以下幾個選項:--requirement、--no-deps、--dest。

這個選項指定下載文件中包含的安裝包,假如有個文件download.txt,內容如下:

當我執行以下命令時:

就會下載文件內的安裝包及其依賴,最後一行輸出如下:

這個選項告訴pip只下載指定的安裝包即可,無需下載依賴,如果我執行以下命令:

就只會下載文件內容中的3個安裝包,最後一行輸出如下:

在我們不指定這個選項時,pip默認把安裝包保存在當前工作目錄,我們可以通過這個選項指定安裝包的保存位置,如下命令:

就會把安裝包保存在當前目錄下的downloads子目錄中了。

Ⅲ python 中如何實現對文件的復制、粘貼

file類中沒有提供專門的文件復制函數,因此只能通過使用文件的讀寫函數來實現文件的復制。這里僅僅給出範例:
src = file("myfile.txt", "w+")
temp = ["hello world! \n"]
src.writelines(temp)
src.close()

src = file("myfile.txt", "r+")
des = file("myfile2.txt", "w+")
des.writelines(src.read())
src.close()
des.close()

shutil模塊是另一個文件,目錄的管理介面,提供了一些用於復制文件,目錄的函數。file()函數可以實現文件的拷貝,聲明如下:
file(src, des)
文件的剪切可以使用move()函數模擬,聲明如下:
move(src,des)
功能:移動一個文件或者目錄到指定的位置,並且可以根據參數des重命名移動後的文件。

Ⅳ 用python寫腳本程序

運行環境:win7 32位 + python3.4

文件名:transmitter.py

內容:

importos,sys,os.path

print("yourcurrentdiris{}".format(os.getcwd()))

iflen(sys.argv)==1:
whileTrue:
sourceDir=input("inputsourcedir:")
ifos.path.exists(sourceDir):
break
else:
print("nosuchdir,tryagain:")
targetDir=input("inputtargetdir:")
eliflen(sys.argv)==3:
sourceDir=sys.argv[1]
targetDir=sys.argv[2]
ifnotos.path.exists(sourceDir):
print("sourcedirdonotexist!")
sys.exit()
else:
print("usage:transmitter[sourcedirtargerdir]")
sys.exit()

ifnotos.path.exists(targetDir):
os.mkdir(targetDir)

cFiles=[fforfinos.listdir(sourceDir)if('.c'infor'.h'inf)]

forfincFiles:
open(os.path.join(targetDir,f),'wb+').write(
open(os.path.join(sourceDir,f),'rb').read())

用法:

pythontransmitter.py[sdirtdir]


針對這個腳本有疑問的可以隨時追問。謝謝

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:705
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:969
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:677
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:830
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:737
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1076
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:308
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:188
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:875
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:829