Hi again.

I want to have a file for one of my python programs, that looks something like this:

<WINDOW> #Window size
640
480
<WINDOW TITLE>
App Platform Test
<ICON>
icon.png

I have a function that reads each line of, and uses the data to make a window. What happens is, if the program reads a line of text from this file (using 'file.readline()'), which is, say, <WINDOW TITLE>, it will read the next line and that will be set as the title.

The problem is with the window size. 'readline()' returns with a newline (\n) thing on the end, and it is also in single qoutes. I can't find a function to remove these so I can use the line as an int.

Please help!
Thanks
Mark

Recommended Answers

All 13 Replies

You can remove the newline at the end of the line with the rstrip method. Also, you can convert a string to int using 'int'

infile = open("infile.txt", "r")
for line in infile:
    line = line.rstrip("\n") # remove the newline at the end
    try:
        line = int(line) # convert to integer if possible
    except ValueError:
        pass # conversion failed. The line is not an integer
    print repr(line) # or do something else

Thank you very much. It works fine now :)

Now I have new problem.
With the icon in the text document in the first post, I use the following code:

line = self.confFile.readline()
        if line == '<ICON>\n':
            line = self.confFile.readline()
            line.rstrip("\n")
            icon = pygame.image.load(line)
            pygame.display.set_icon(icon)

Python just says that it can't open icon.png. I have checked the source folder and everything is there. I've tried putting qoutes and double qoutes around the icon path in the text document but none of that works.

Strange... try this

import os.path

#.... your code here
 line = self.confFile.readline()
        if line == '<ICON>\n':
            line = self.confFile.readline()
            line.rstrip("\n")
            if not os.path.isfile(line):
                print("FILE DOES NOT EXIST %s" % repr(line))
            icon = pygame.image.load(line)
            pygame.display.set_icon(icon)

and post the output here.

FILE DOES NOT EXIST 'icon.png\n'
Traceback (most recent call last):
  File "C:\Users\TN30RY\Documents\Programming\Python\Projects\App Platform\Platform001\src\main.py", line 74, in <module>
    app = appPlat()
  File "C:\Users\TN30RY\Documents\Programming\Python\Projects\App Platform\Platform001\src\main.py", line 15, in __init__
    self.readConfig()
  File "C:\Users\TN30RY\Documents\Programming\Python\Projects\App Platform\Platform001\src\main.py", line 46, in readConfig
    icon = pygame.image.load(line)
pygame.error: Couldn't open icon.png

Wha?
Here is my src folder.

src/
	doc.txt    #Text document
	error.ico 
	icon.png #I have paint.net and PNG is stated as Paint.net image? It still has .png extension
	main.py
	success.ico
	warning.ico

Also, it still has the newline chars on the end (On the FILE NOT EXIST thing).

The \n is still there ! You should write

line = line.rstrip("\n")
# or line = line.strip() to remove all white space at both ends

Nope. In your code, which I copied and pasted in, the line.rstrip("\n") bit was in there already. I use line.strip() but that didn't work...

Nope. In your code, which I copied and pasted in, the line.rstrip("\n") bit was in there already. I use line.strip() but that didn't work...

line.rstrip() doesn't do anything at all to line
it returns a string that is the rstripped version.
gotta remember that.

use
line = line.rstrip()

yes i no. Look at Gribs code. It has line = line.rstrip(). I just cba 2 write out the whole bit in my post

solv-èd

I just changed it to JPEG format and it works lol.

File "C:\Users

Use "File "C:\\Users" or C:/Users

that has nothing to do with it

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.