Problems with files

18 May, 2013

Evan writes:

I have a Windows 8 PC, and have installed Python 3.3 onto it, and worked my way up to the last part of chapter 9 in your book with no major problems. But now I am stuck in the last part of chapter 9. The first time I tried to save a notepad txt file to the C: location, access was denied, because the user account I was using was not the dominant user account. I fixed this by running the notepad as the administrator, and was able to save the txt file to the C: location. Opening and printing the contents of the txt file worked fine after that. My next problem (and still is a problem) was writing to the text file. The first error message said access was denied. I fixed this also by going to the administrator's user account and allowed the user settings I was working on to have almost complete control over all the C: location. Now when I try to write to the txt file, I get a different error: "OSError: [Errno 22] Invalid argument: 'c:\myfile.txt'. Please help me!

Hi Evan. The final error you're getting is generally caused by an invalid filename (for example, if you had two colons rather than one) - but I don't see that problem in the example you sent. So I'm not quite sure what's gone wrong there.

In terms of the example in the book, I've added a new entry in the errata (see the section Page 123). The directions should instead tell you to save the file to your user directory - something like: c:\\Users\\Evan - rather than to c:\. Check the errata and hopefully that should work for you.


Tkinter colorchooser problems

01 May, 2013

Michal writes:

My son has found a problem. It is regarding:

from tkinter import *
colorchooser.askcolor()

It is not working and I cannot help him. On the python shell he gets an error:

Traceback (most recent call last):
File "colorrect.py", line 2, in <module>
colorchooser.askcolor()
NameError: name 'colorchooser' is not defined

Could you help please.

This is an odd one. If I try the code in the Shell, the color chooser is available as expected:

colorchooser1

But if I try the same code in the command-line console, I get a similar error:

colorchooser2

The only way I can reproduce the problem in the Shell is when I don't run it in "No Subprocess" mode - that's if you don't follow the installation instructions, in Chapter 1 of the book, and just run the Shell/IDLE as it was installed (without modifying the shortcut):

colorchooser3

You can get around this, by directly importing the colorchooser module like so:

>>> import tkinter.colorchooser
>>> tkinter.colorchooser.askcolor()

But you'll probably hit problems if you try to call the askcolor function (the second line in that snippet of code above).

All of which is rather messy and confusing. My suggestion would be to make sure you see the "No Subprocess" message when you run IDLE, and if not, revisit Chapter 1. If you do see the message on startup, and still get the error (which I haven't managed to do, but I'm guessing might be possible), try the sample code above, and see if that works for you. Good luck!

More information updated July 2020 here.


Good luck

24 Apr, 2013

To the (I assume) wannabe-hackers who, according to my web server logs, are trying to access various combinations of wp-admin, wp-login and other types of login page... good luck with that! ツ


Python on the Web?

17 Apr, 2013

Dean writes:

I'm 13, I live in the US, and I just finished reading your excellent book "Python for Kids". After I finished the book I had two questions. First I was curious if it is possible to put a python game on a website to play? The other question I had was what was the hardest project you have ever worked on, how long did it take you, and what language was it in?

There are a few implementations of Python that will work inside the browser, but as far as I know they don't support all of Python's features (such as the tkinter module, or PyGame, for example). So it would be rather difficult to write a game that could be played on a website (in my opinion). If that's something you want to do, you'd be better off learning Javascript (the standard programming language used in all browsers). If you google for "javascript and html5 games development", you'll find a lot of information out there. Luckily, once you've learned one programming language, it's a lot easier to learn the next.

In terms of hardest project, that's a difficult question to answer. I've worked on projects which were hard because the amount of code in the application was huge, and because there were a lot of "moving parts" - lots of communication between different systems. In terms of complicated systems, I recall one project (which I spent about 18 months on with a team of 8 or 9 people) where I had to write the code for very complicated graphical reports -- that was hard because I had to remember mathematics I learned years before (the programming language in that case was Java and some Python as well).

Hope that helps.


Solution 6

14 Apr, 2013

The solution to programming puzzle #6 has just been posted.