Why don't either of these implementations actually write to the file?

Code v1:

txtfile = open('C:/Python31/SKUs.txt', 'w')

with txtfile:
    num = 817
    while num <= 820:
        x = "G" + str(num)
        print(str(x), end="    ")
        num += 1

txtfile.close()

Code v2:

txtfile = open('C:/Python31/SKUs.txt', 'w')

num = 817
while num <= 820:
    x = "G" + str(num)
    print(str(x), end="    ", file = txtfile)
    num += 1

print("test", file = txtfile)

txtfile.close()

Also, I could use some further help.

Here's what I want each page to look like (note the big font size of the SKUs relative to the page size, and the spacing):

-----------------------------
| G817      G818       G819 |
|                           |   <-- page 1
|                           |
|                           |
|___________________________|
-----------------------------
| G817      G818       G819 |
|                           |   <-- page 2
|                           |     (and so on, until G10000)
|                           |
|___________________________|

I don't know how to make the font-size huge, or to achieve this spacing through Python code. Nor do I know how to create page breaks. If someone could help me with this that'd be great!

I'm also interested in knowing how to change fonts, and text colors (although it's not necessary for this project).

Additionally, if anyone knows of a good tutorial on writing to files, that'd be greatly appreciated!

You'll need to actually write to your file handle txtfile by using txtfile.write( text ) .

Okay, I changed it to the following, but when I run it and open the file, the document is still blank.

txtfile = open('C:/Python31/SKUs.txt', 'w')

with txtfile:
    num = 817
    while num <= 820:
        x = "G" + str(num)
        txtfile.write(x)
        num += 1

txtfile.close()

Okay, I changed it to the following, but when I run it and open the file, the document is still blank.

txtfile = open('C:/Python31/SKUs.txt', 'w')

with txtfile:
    num = 817
    while num <= 820:
        x = "G" + str(num)
        txtfile.write(x)
        num += 1

txtfile.close()

That code works fine for me. What platform are you on?

That's rather unnerving. I'm using vanilla Python 3.1 on Windows XP.

This shouldn't make a difference but the proper way to use the with statement is like so:

with open('C:/Python31/SKUs.txt', 'w') as txtfile:
    num = 817
    while num <= 820:
        x = "G" + str(num)
        txtfile.write(x)
        num += 1

Notice I omit closing the file. That's because the with syntax automatically closes the file handle. AFter that loop you can test with txtfile.closed , which should return True because it's closed.

Nice! Okay thanks that's helpful.
Hmm.. I tried running the code on my mac (Python 3.1) and it yields no results either.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.