Working with Files – Python

A couple weeks ago one of my wife’s nice told her that there is a Costco store in Minneapolis that seems to sell different items and in larger packages than others. The Costco Minneapolis Business Center Warehouse is located at 3311 Broadway St NE, Minneapolis, MN 55413-4553. We have not visited the store yet.

A couple days ago there was an article by CBS News titled Groceries could see meat shortages by end of week amid plant closings. I read it and twitted it. In addition I sent a message with the link to my two sons. Apparently they both decided to resupply on meats yesterday. One of my sons lives in Madison, WI so he just went to the Costco store near him. The other lives in MN. The one in WI periodically orders a side of beef and a side of pork. I believe he should be fine for a while.

My son who lives in MN decided to visit the Costco store on Broadway Street. He was impressed by the size of the packages of meat. The prices were very appealing. The only drawback for some might be that you need to buy a large and uncut piece (e.g., full rib eye) and when you get home you need to cut in servings, put them in plastic bags, and then store them in a freezer. If you do not do so and just put it in a freezer, you will have to consume it all in a few days after thawing it. You should never refreeze meats. My sons and we at home are used to this process. We get most of our protein at restaurant suppliers.

My son did mention the lack of chicken. Apparently when he visited the Costco store they were out of chicken. Apparently he got a couple different cuts of beef.

At home we are doing fine with protein. We stock periodically and typically our supplies last several months. Hopefully all will return to the new normal after the COVID-19 pandemic before we run out of protein in the freezers.

As you know I am in the process of refreshing Python and studying machine learning. I received a message from Microsoft suggesting a set of YouTube videos on Channel 9 Microsoft Developer. If you work with Azure and Microsoft products, I would recommend the channel. You never know what you will learn.

I watched and experimented while doing so, Working with files | More Python for Beginners [14 of 20]. With time Python is growing and there are so many libraries. You should always do a search before starting on a specific project or task. You might be surprised on what you find.

I used to work with Pearl when I needed to write a script. With time I used it for product components and to implement features. Python seems to have replaced Pearl and the power and simplicity are just amazing.

In this post we will read and write text files.

Microsoft Windows [Version 10.0.18363.778]
(c) 2019 Microsoft Corporation. All rights reserved.

# **** type a random text file ****
C:\Users\johnc>type c:\temp\README.txt


Windows 10 Couldn't start client Java Language Server even with java_home set #785
https://github.com/redhat-developer/vscode-java/issues/785


c:\temp\jdk-8u241-windows-x64.exe
c:\Program Files\Java\jdk1.8.0_0241


c:\temp\jre-8u241-windows-x64.exe
C:\Program Files\Java\jre1.8.0_241


in Path:
C:\Program Files (x86)\Common Files\Oracle\Java\javapath;
C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-hotspot\bin;
c:\sencordist\bin;
c:\sdm\bin;
%SystemRoot%\system32;
%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\npp.7.7.1.bin.x64;C:\Program Files\Azure Kinect SDK v1.2.0\tools;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Git\cmd;


C:\Users\johnc>where java
C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe
C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-hotspot\bin\java.exe

From a command console we display the contents of the README.txt file. I generated this file while updating Java for the Visual Studio Code IDE from Microsoft. Disregard the actual contents of the file. Just note that some lines are blank while others have some text.

# **** read the contents of the specified file ****
fileName = 'c:\\Temp\\README.txt'
read_file(fileName)

# **** name of file to write ****
fileName = "c:\\Temp\\WRITEME.txt"

# **** check if the speficied file exists ****
exists = os.path.exists(fileName)
print(f"exists: {exists} fileName ==>{fileName}<==")

# **** remove file (if present) ****
if exists == True:
    os.remove(fileName)

# **** write the specified number of lines and list to the specified file ****
count = 10

list = []
list.append("James")
list.append("Bond")
list.append("007")
list.append("- License to Kill")

write_file(fileName, count, list)

This Python code implements the operations that we will perform with two files. We start by specifying a name and reading the file. Note that the same function will display the contents of the file. We will see that shortly.

We then specify a file to be written. We check to see if the file exists. If it does we remove the file. Note that we could have achieved the same results in different ways. We are just experimenting. If the file exists we remove it.

We then set a number of lines to write. We also declare a list and populate it with some words. Finally we write some text to the specified file.

# **** read the specified text file ****
def read_file(fileName):

    # **** open the specified text file ****
    try:
        stream = open(fileName)
    except OSError:
        print(f"open fileName ==>{fileName}<== exception OSError") os._exit(-1) except: print(f"open fileName ==>{fileName}<== failed") os._exit(-1) # **** check if stream is NOT readable **** if stream.readable() == False: print(f"fileName ==>{fileName}<== not readable") os._exit(-1) # **** read and display the contents of the text file **** count = 0 while True: # **** read the next line from the file **** line = stream.readline() # **** check if the line is empty (end of file) **** if not line: break # **** remove trailing CR from the line **** line = line.rstrip() # **** skip this line (if empty) **** if line == "": continue # **** display the line **** print(f"line ==>{line}<==")

        # **** count this line ****
        count += 1

    # **** close the specified text file ****
    stream.close()

    # **** display the number of lines displayed ****
    print(f"\ncount: {count}")

The read_file function opens the specified file. Note that it is possible for the file to not be available. If the file fails to open our code throws an exception. You can experiment with this section by using a file name for a non existing file.

Once we find the file, we check if we can read it. For simplicity we just decided to display a message and return. Probably we should throw an exception in order to be consistent.

We enter a loop. We read a line of text from the file. If there are no more lines (end of file) we break out of the loop.

When a line of text is read the CR \ LF character(s) are also included. We would like to remove it / them.

Once that is accomplished we check for empty lines and skip them.

If all is well so far, we display the line with additional text.

When all is said and done with this pass we increment the count of lines. We could use the count to display it when we display the contents of a line. Like I said, when you experiment you tend to add and remove code until you are satisfied with the solution you have developed.

When the file has been read, we close the file and display the line count. Note that we are not counting blank lines. By moving the count += 1 statement up, we could count all lines.

# **** write the specified text file ****
def write_file(fileName, count, l):

    # **** open the specified text file ****
    try:
        stream = open(fileName, "at")
    except OSError:
        print(f"open fileName ==>{fileName}<== exception OSError") os._exit(-1) except: print(f"open fileName ==>{fileName}<== could not open")
        os._exit(-1)

    # **** write a set of lines ****
    for i in range(count):
        stream.write(f"this is line {i} in this file\n")

    # **** write the contents of the specified list ****
    stream.writelines(' '.join(l))
 
    # **** flush the stream (not needed in this case) ****
    stream.flush()

    # **** flush and close the file ****
    stream.close()

The write_file function opens a file for appending. We know that the file does not exist because it was earlier deleted. We could / should have opened the file for writing.

We write a set of lines as specified by the count.

We follow by writing the words in the list in a single line. Note that the brackets and separating commas are not displayed.

Finally we flush the file and close it. There is no need to flush and close because the close method also flushes the file contents.

ine ==>Windows 10 Couldn't start client Java Language Server even with java_home set #785<== line ==>https://github.com/redhat-developer/vscode-java/issues/785<== line ==>c:\temp\jdk-8u241-windows-x64.exe<== line ==>c:\Program Files\Java\jdk1.8.0_0241<== line ==>c:\temp\jre-8u241-windows-x64.exe<== line ==>C:\Program Files\Java\jre1.8.0_241<== line ==>in Path:<== line ==>C:\Program Files (x86)\Common Files\Oracle\Java\javapath;<== line ==>C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-hotspot\bin;<== line ==>c:\sencordist\bin;<== line ==>c:\sdm\bin;<== line ==>%SystemRoot%\system32;<== line ==>%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\npp.7.7.1.bin.x64;C:\Program Files\Azure Kinect SDK v1.2.0\tools;C:\Program Files\Microsoft SQL Server\Client 
SDK\ODBC\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\DTS\Binn\;C:\Program Files\Git\cmd;<== line ==>C:\Users\johnc>where java<== line ==>C:\Program Files (x86)\Common Files\Oracle\Java\javapath\java.exe<== line ==>C:\Program Files\AdoptOpenJDK\jdk-11.0.6.10-hotspot\bin\java.exe<== count: 16 exists: True fileName ==>c:\Temp\WRITEME.txt<==

We see the output of our program. Only the lines with text were displayed. The count of non-blank lines is 16.

It seems that the WRITEME.txt file existed before we ran the program. It had to be deleted before writing to it. If the operation would have failed to delete the file, we would have appended the text.

Microsoft Windows [Version 10.0.18363.778]
(c) 2019 Microsoft Corporation. All rights reserved.

# **** ****
C:\Users\johnc>cd c:\temp

# **** ****
c:\Temp>type WRITEME.txt
this is line 0 in this file
this is line 1 in this file
this is line 2 in this file
this is line 3 in this file
this is line 4 in this file
this is line 5 in this file
this is line 6 in this file
this is line 7 in this file
this is line 8 in this file
this is line 9 in this file
James Bond 007 - License to Kill

From a command prompt we display the contents of the file we wrote. It seems like all is working as expected.

The entire code for this project can be found in my GitHub repository.

If you have comments or questions regarding this, or any other post in this blog, or if you would like for me to serve of assistance with any phase in the SDLC (Software Development Life Cycle) of a project associated with a product or service, please do not hesitate and leave me a note below. If you prefer, send me a private message using the following address:  john.canessa@gmail.com. I will reply as soon as possible.

Keep on reading and experimenting. It is the best way to learn, refresh your knowledge and enhance your developer toolset!

One last thing, many thanks to all 754 subscribers to my blog!!!

John

Twitter:  @john_canessa

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.