1. 程式人生 > >Python獲取當前檔名 讀檔案

Python獲取當前檔名 讀檔案

import sys
import os

# 絕對路徑
print(__file__)
print(sys.argv[0])

# 檔名
print(os.path.basename(__file__))

print(os.path.basename(sys.argv[0]))

if len(sys.argv) >= 2:
    name = sys.argv[1]
    name,txt = name.split('.')
    print(name,txt)
    with open(sys.argv[1],'r') as f:
        aa = f.read()
        content = aa.replace(' - '
,'-') with open('{}1.{}'.format(name,txt),'w') as ff: ff.write(content)