python windows 檔案重新命名

2022-09-04 13:48:16 字數 607 閱讀 2499

windows系統,在同乙個資料夾內,無法命名乙個相同檔名的檔案。linux可以直接重新命名,並會替換掉之前的檔案。

所以要先刪除檔案後在重新命名,並且要獲取檔案路徑。

import os

from nt import chdir

def renamer(new_file,old_file):#刪除檔案,並且用刪除檔案的檔名,重新命名另外乙個檔案

os.remove(old_file)

chdir(os.path.dirname(new_file))

os.rename(new_file, old_file)

account_file = 'e:/1.txt'

account_file2 = file('e:/1.txt','w+b')

account_file2.close()

new_f = file('e:/2.txt','w+b')

new_f.close()

new_ff = 'e:/2.txt'

renamer(account_file, new_ff)

python windows下面環境配置

我選擇的是python2.7 直接雙擊安裝,然後再環境變數path中加上python的目錄比如 e python27 然後再cmd資料python,應該就成功了。2.安裝easy install 如果是ez setup.py,直接雙擊執行。easy install安裝以後在e python27 sc...

python windows路徑轉Linux路徑

參考 python windows路徑轉linux路徑 例如筆者需要獲取當前指令碼 test2.py 所在的路徑,並將該路徑轉換為linux下的路徑 import os import sys if name main print this is test2 file sh path os.path....

python windows 定時任務

之前都是在liunx上部署任務,現在換到windows部署python計畫任務,期間遇到一些小問題,現在總結一下 1 執行結果 0x1 程式或指令碼 python.exe 新增引數 可執行檔案的絕對路徑 起始於 python.exe的所在路徑 2 python檔案內的引用和檔案儲存問題 需要把檔案的...