简体   繁体   中英

OS.PATH.EXISTS returning false positive

I'm having a problem with my code, this problem is that it keeps telling me the file already exists when it doesn't.

Here's the code

http://codepad.org/ENZDzGhz

The code is simply for a login system I have to my simulation game. And the If Statement keeps returning true and not creating the account for me.

This is not an answer. I'm just so fed up that I can't post code snippets in comments and I don't know any way around it.

As soon as you commented on my answer I will remove this non answer (Any moderator being faster than me can do the same)

You have following code snippet

if(os.path.exists("{}.json".format(rUsername))):
            print(os.path.exists("{}.json".format(rUsername)))
            print("{}.json".format(rUsername))
            print("Account already exists!")
            return;

with open("{}.json".format(rUsername), "w+") as rUser:
       info = {
       "Username": rUsername,
       "Password": rPword
       }
       x = json.dump(info, rUser, indent=4);
       rUser.close()

As a general recommendation I suggest to not 'create' a file name in multiple lines. just create it once. This might help to avoid some silly bugs because of typos / unicode errors or similiar.

Just try following snippet and look whether the print statements help you understand

        fname = "{}.json".format(rUsername)
        if os.path.exists(fname):
                    print("os.path.exists =", os.path.exists(fname)))
                    print("Account already exists! I found file", repr(fname))
                    print(repr(fname), "has a size of", os.path.getsize(fname), "bytes")
                    return

        with open(fname, "w") as rUser:
            info = {
                "Username": rUsername,
                "Password": rPword,
            }
            x = json.dump(info, rUser, indent=4)

        print(repr(fname), "has a size of", os.path.getsize(fname), "bytes")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM