Writing and reading python files on the debain system of the DragonBoard 410c development board

Recently, a function of reading and writing files was developed using python on the debain system of the DragonBoard 410c development board. The specific code is as follows:

# -*- coding: utf-8 -*-
import  sys,os
StrFilePath = os.path.dirname(os.path.realpath(__file__))+"testFile.txt"
def WriteToFile(str):
    print(StrFilePath+"||"+str)
    try:
        FileHandle = open(StrFilePath, 'w')
        FileHandle.write(str)
        FileHandle.close()
    finally:
        if (FileHandle):
            FileHandle.close()

def PrintFile():
    try:
        FileHandle = open(StrFilePath,"r")
        while True:
            chunk = FileHandle.readline()
            if not chunk:
                break
            print(StrFilePath+chunk)
        FileHandle.close()
    finally:
        if(FileHandle):
            FileHandle.close()

Two methods are provided in the code

In the writeToFile method, first use the open method to open the file StrFilePath for writing, and then call the write method to write the incoming str string to the file.

In the PrintFile method, the open method is also used to open the file StrFilePath in read-only mode, and the readline method is called to read the file content.


The external only needs to call these two methods through the python file name to read or write the file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324832587&siteId=291194637