Mostly Linux & Python syntax notes and hyperlinks.

Wednesday, September 16, 2009

Windows Explorer XP 5.1 Windows often hangs when I open a file

The issue is that my windows explorer (what a Mac user would call the Finder Window) keeps hanging on me.  In the folder-name bar, it has the folder name, then "(Not Responding)". I end up clicking the red X at the top right to close it, then selecting 'End Program', letting it start up again, and then opening a new windows explorer and navigating my way back to the folder.  This happens often when I double click to open a file.  It happens frequently and is getting annoying.

Someone suggested that I defragment my hard drive.  I've run the defragger over and over until there's not much left to de-frag.  No help.

Search on Microsoft site didn't help.
Attempt to do a Microsoft updatate didn't show any patches for XP 5.1, service pack 3.

Google search resulted in http://www.analogduck.com/main/explorer_hangs  which links to http://www.annoyances.org/exec/forum/winxp/t1167888467

I followed the advice from http://www.annoyances.org/exec/forum/winxp/t1167888467 here:
To stop ctfmon from autostarting and running all the time:
Control Panel>Regional and Language Options>Languages>Details>Advanced,
 and check the box that says "Turn off advanced text services." 
And I've gone into the Task Manager and stopped ctfmon.exe.

I've gone into the Control Panel/Administrative Tools/Services window and stopped the SSDP Discovery Services (along with other services that I didn't think I needed.)

Let's see if it helps...

2:56pm.  The (Not Responding) happened when I tried to open a text document by double-clicking, but it didn't last as long.
7pm.  No, I'm still getting the hanging when I try to double-click to edit a text file.  And I have re-booted since the previous changes.
Oh well.

Tuesday, September 15, 2009

python day 2: functions, if, else, TypeError

To create a function, define it before you need to use it, and don't forget the colons. Not for the if either. (Python seems to like colons. It's kind of like how C likes semicolons, and Lisp likes parentheses.)

import os, sys

def run_cmd(cmd,fd):
try:
if sys.platform.startswith("win")
print cmd
fd.write('running cmd from win: '+cmd+'\n')
EXITCODE = os.system(cmd) >> 8
#Here's where I got the TypeError:fd.write('cmd returned '+EXITCODE+'\n')
else:
import popen2
process = popen2.Popen3(cmd)
EXITCODE = process.wait() >> 8
return EXITCODE
except:
print "run_cmd: Some Exception Caught."
fd.write('run_cmd: Some Exception Caught\n')

The TypeError complained:

    TypeError: cannot concatenate 'str' and 'int' objects

I didn't realize Python was so fussy about types. I thought it was a cavalier sort of Javascript kind of interpreted thingy. OK, so to solve that...it looked like I needed to use a sort of C syntax with the print statements, but that wasn't quite right. I tried it, but got another TypeError:

      fd.write('cmd returned %d\n',EXITCODE)
TypeError: function takes exactly 1 argument (2 given)

Oh, wow, this really is fussy. Experimenting with the shell again:

    >>> num=3
>>> num
3
>>> print "%d" %(3)
3
>>> print "num=%d" %(num)
num=3
>>> print "num=%d" % num
num=3

OK, so it's not C/Java. I need to say:

    fd.write('cmd returned %d\n' % EXITCODE)

Now it works.

One weird thing is that the tutorial I've been using has both %d and %i listed as "signed decimal integer". The python.org page on string formats only lists the 'd' as an integer type, so I'll stick with that. Researching further, there's a section on String Formatting Operations that lists both d and i again. Must be some historic reason for it...

Thursday, September 10, 2009

starting python: file I/O, strip(), [start:end]

Janak left me with a python script to process a directory full of files. He said I'd have to modify it a little. He went home.

First thing to look up: To add a comment, put a "#" at the start of the line.
Useful for note-taking in the code, and commenting out the complex stuff while I try to get the easy stuff working.

Well, I have a file I'm writing to:
fd=open("list.txt","w+")
fd.write(var+' is the value of var.\n')
And a file I'm reading from :
for line in open(from_dir + "\\" +in_file)
That is cool to have the read-line from the file in the same step as the opening of the file, and all in a for loop while the lines in the file don't run out, all in one step. And what that syntax does is obvious even to a novice.

I'm not sure about the "\\". This is in Windows, but why two backslashes? Is the first \ to escape the second one? In Linux, would it be "\/" or "/"? Is there a unix/windows-neutral version?

Next, he keeps using this strip() function
line=line.strip()
Here's the official syntax: http://docs.python.org/library/string.html
In a tutorial: http://www.tutorialspoint.com/python/string_strip.htm
If you don't put anything in the brackets, it strips the whitespace from the start and end of the string.

He also uses square brackets to pull out substrings from our lines of data.
The negative value in the range is hard to figure out, but it means "start counting from the end".
If you get a python prompt, you can experiment with square bracket syntax.
It's good to see that it doesn't freak if you try to pull out an impossible range. It just returns an empty string.
>>> s1="abcde.fgh"
>>> s1
'abcde.fgh'
>>> s1[0:2]
'ab'
>>> s1[0:-4]
'abcde'
>>> s1[2:3]
'c'
>>> s1[2:4]
'cd'
>>> s1[-4:-2]
'.f'
>>> s1[-2:-4]
''
>>> s1[5:-3]
'.'
If you're testing the start or end of a string, don't use ranges.
Use startswith and endswith instead:
if str.endswith(".txt"):
if str.startswith("my_files_"):
You'll avoid goofs from miscounting characters, and it's also easier to read and understand.