Even though it in the same directory as the Python file it cannot be red
from datetime import datetime
infile = r"./system.log.txt"
logFile = []
def readLog():
with open(infile) as f:
f = f.readlines()
for line in f:
temp= line.split('\n')
logFile.append(temp[0])
def show():
for line in logFile:
print(line)
I am assuming you have this sructure
folder
|--sys.log.txt
|--main.py
cd folder
then python main.py
file='system.log.txt'
with open(file,'r') as f:
print(f.read())
Check the path of your file against what gets printed from the code below.
import os
print(os.path.dirname(__file__) + "/system.log.txt")
Also for loop needs to be indented properly.
import os
infile = os.path.dirname(__file__) + "/system.log.txt"
logFile = []
def readLog():
with open(infile) as f:
f = f.readlines()
for line in f:
temp= line.split('\n')
logFile.append(temp[0])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.