Python Single-Point Geometry Energy Script Update

Continued from a previous post.

As I created my first completed version of the python script, I learned several things about python.

One thing I learned was how file handles work. With the function:

def find_parameters(file_handle):
    for l in file_handle:
         if l.startswith(" #"):
             return l.rstrip()

I was running into an error in which I received the message, Nonetype has no attribute…, meaning that my code was trying to use nothing as its input for functions that were looking for strings. That was after I looped through the file handle for a find function. I learned that looping through a file handle leaves the “cursor” at the end of the file handle, so that the very next useĀ of the file handle would start at the end of the file handle. To reset the “cursor” at the beginning of the file handle, I need to call after each iteration, as follows:

def find_parameters(file_handle):
    for l in file_handle:
         if l.startswith(" #"):
             return l.rstrip()

Here is a Stack Overflow thread with an explanation.

Another thing I learned had to do with the difference between the .append() and .extend() functions. After some searching and consultation with Prof. Kennerly, I realized that that was because I called .extend() rather than .append(). I was trying to add a string as an item to an empty list, and the .extend() function treated the string itself as some sort of list. Using the .append() function instead kept the string as a string and simply added it as a value to the list. Here is a Stack Overflow thread with more discussion of the difference between .append() and .extend().

One more error that I do not understand but have since overcome has to do with Unicode. I had an interesting hour or so in which I got long text files of Sanskrit and other unexpected languages instead of my output data, when I opened my results text file in Wordpad. Other text editors, such as Microsoft Word, had no trouble reading the text files. I think that this error had something to do with the .extend() function, but I’m not sure. I recall that the error went away after I corrected my code to use .append() instead of .extend(), but that may not have been the direct source of the error. Further investigation of the Unicode issue is needed for a better understanding of it.

Often error messages may not describe the cause of the problem, but give clues of where the problem is. That is why many different error messages popped up when I had a more fundamental issue somewhere else in the script. In addition, as Prof. Kennerly says, computers always do what you tell them. So, the Sanskrit output made sense, given the code I wrote. To fix problems with the script, I needed more understanding of what certain functions did and how python and Wordpad read files.

I used IDLE on a Windows computer to edit and run my script.

P.S. WordPress is not good at allowing indentations or extra spaces. I have to use a keyword &+nbsp; (without the +) to create spaces in the text editor, and all instances of it disappear if I switch to the visual editor. I think this is an issue with HTML, and I read that I would have to use CSS to allow indents in WordPress. This is an annoying problem, and I am trying to get around it for future posts where I show snippets of code.


Filed under Uncategorized

4 Responses to Python Single-Point Geometry Energy Script Update

  1. In your first para, it might help clarify what loop with a file handle was failing by showing a short code snippet, and also showing how a file handle is declared . And by ‘consecutive loop of the file handle’ I think it is clearer to say ‘the very next use of the file handle’

    Also there’s a handy HTML to set off code which makes these post easier to read, use: like this

    I forget now the exact Unicode issue. It wasn’t exactly python’s fault. It was more word pad’s (or whatever editor you were using) was assuming the file encoding was one thing, when it was really another. I think the .extend() and .append() was a separate issue, where .extend() converted the string to individual characters.

    Good links to relevant Stack overflow threads.

    You could probably write a whole other post about IDLE vs. PyCharm once you get more used to them.

  2. Pingback: Python: Dealing with Incomplete Files | Computational Chemistry at Skidmore College

  3. Pingback: Python Error Handling: Temporary Files and Skipping Failed Jobs | Computational Chemistry at Skidmore College

Leave a Reply