简体   繁体   English

Python 脚本不创建文件

[英]Python Script doesn't create file

So I tried making something like a password vault, where the file pw.txt should be created if it doesnt exist.所以我尝试制作类似密码保险库的东西,如果文件 pw.txt 不存在,应该在其中创建它。 For some reason it doesnt work.由于某种原因,它不起作用。 Please help guys!请大家帮忙!

import os.path
import os


def create_file():
    if os.path.exists("D:\\Libaries\\Documents\\resources_py\\pw.txt"):
        pass
    else:
        file = open("pw.txt", 'w')
        file.close()


create_file()

I already found my mistake I forgot to change the path here:我已经发现我的错误我忘了在这里更改路径:

from

file = open("pw.txt", 'w')

to

file = open("D:\\Libaries\\Documents\\resources_py\\pw.txt", 'w')

Still thanks for the quick reply!仍然感谢您的快速回复!

Ran the code and it seemed to work just fine for me, the only difference possible is that the os.path.exists() is returning as true which means the file wouldn't be created.运行代码,它似乎对我来说工作得很好,唯一可能的区别是 os.path.exists() 返回为 true,这意味着不会创建文件。

You should probably look at what the os.path.exists() is returning after you run the function and go from there从那里运行 function 和 go 后,您可能应该查看 os.path.exists() 返回的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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