一個很簡單的小程式,出現奇怪問題
目的:使用pathlib.Path方法實作對“/A/B/C/D/E/F/G/H/”下所有“QAM_MCS_8150.txt”以及“RI_Layers_8150.txt”兩個檔案夾中部分欄位進行replace操作,并保持檔案名不變。
問題:Pytharm運行0錯誤,但Pycharm始終提示所有帶File的欄位都標黃(未進邏輯里),很奇怪。
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#!/usr/bin/env python3
# _*_coding:UTF-8_*_
# Author: [email protected]
# time:31/3/2020
import os
import re
import pathlib
from pathlib import Path
p = Path('/A/B/C/D/E/F/G/H/')
for file in p.rglob('*.txt'):
if 'QAM_MCS_8150.txt' in file:
with open(file, 'r') as filereader1:
with open(file.replace('.txt', '_new.txt'), 'w') as filewriter1:
for row in filereader1:
if "256QAM" in row:
row = row.replace('256QAM', '8')
elif "64QAM" in row:
row = row.replace('64QAM', '6')
elif "16QAM" in row:
row = row.replace('16QAM', '4')
elif "QPSK" in row:
row = row.replace('QPSK', '2')
elif ",31," in row:
row = row.replace(',31,', ',,')
elif ",30," in row:
row = row.replace(',30,', ',,')
elif ",29," in row:
row = row.replace(',29,', ',,')
elif ",28," in row:
row = row.replace(',28,', ',,')
filewriter1.write(row)
print('QAM and retransmission have replaced done...')
os.remove(file)
if 'RI_Layers_8150.txt' in file:
with open(file, 'r') as filereader2:
with open(file.replace('.txt', '_new.txt'), 'w') as filewriter2:
for row in filereader2:
if "Rank " in row:
row = row.replace('Rank ', '')
filewriter2.write(row)
print('Rank has replaced done...')
os.remove(file)
for file in p.rglob('*_new.txt'):
os.rename(file, file.replace('_new.txt', '.txt'))
print("Rename done")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uj5u.com熱心網友回復:
自己頂上去呢轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/62295.html
