[Python] Why is python.strip() not working for file paths? [duplicate]

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    What I'm trying to do is import a file using open() in python. This works fine, and inside the text file is a bunch of other paths that lead to other files in the same folder, which I should then be able to access. However, when displaying this in the pygame window my game is on, I want the user only to see the file name, not the full path and file extension (.txt). However, when I strip the full string of the newline, the path and the extension, some of the name is stripped even though I never specified so. Here is my code:

    for level in enumerate(all_levels, 1): #all levels is a list containing the paths for the levels; for example, "C:\Users\HelloWorld\MyGame\level1.txt\n"
    levelstripped = level[1].strip('\n').strip('C:\\Users\\HelloWorld\\MyGame\\').strip('.txt')
    text = font.render(levelstripped, True, (255, 0, 100))
    canvas.blit(text, (600-(len(newlevel)*10), 37.5+(level[0]*100)))


    The problem is, the text blitted to the canvas (basically the universal surface for the game) shows 'vel1' instead of 'level1'. The coordinates of the text are made to centre it depending on its length and put it in a different position so multiple texts aren't layered on top of each other.

    Continue reading...

Compartilhe esta Página