简体   繁体   English

当我运行它的 .py 文件时,无法在 python 中写入文件。 但是当我在pycharm中运行它时,它确实有效

[英]Can't write files in python when I run it's .py file. But when I run it in pycharm, it actually work

I'am still new in python and im having a hard time why does my py file wont write text when i run it its .py file.我还是 python 的新手,我很难理解为什么我的 py 文件在我运行它的 .py 文件时不会写入文本。 But whenever i run it on pycharm it always works.但每当我在 pycharm 上运行它时,它总是有效的。 I also tried many option when writing on a file and it still won't write anything in file unless i run it on Pycharm.在写入文件时我也尝试了很多选项,除非我在 Pycharm 上运行它,否则它仍然不会在文件中写入任何内容。 Am i missing something?我错过了什么吗? pleasee need help请需要帮助

here the .py file im telling whenever i run this, nothing happens这里的 .py 文件告诉我每当我运行它时,什么都没有发生

path = 'wifipasswords.txt'
my_open = open(path, 'w+')
my_open.write(final_output)
print(final_output)
my_open.close()


//MY attempts
# with open("wifipasswords.txt", "w") as f:
#     print(final_output, file=f)

# pathlib.Path("wifipasswords.txt").write_text(final_output)
# with open("wifipasswords.txt", "w") as f:
#     f.write(final_output)

# file = open("wifipasswords.txt", "w")
# file.write(final_output)
# file.close()

Make sure that:确保:

  1. you are running python main .py你正在运行 python main .py
  2. that you don't have python2 and python3 installed together, resulting that maybe "python.exe" in PATH variable is actually python2.你没有一起安装python2和python3,导致PATH变量中的“python.exe”实际上是python2。 If you do, edit path variable.如果这样做,请编辑路径变量。

This is almost certainly an issue of your working directory.这几乎可以肯定是您的工作目录的问题。 The location of the script does not imply that that is where the file will be created, and you're probably looking in the wrong place for the resulting file.脚本的位置并不意味着文件将在此处创建,您可能在错误的位置查找生成的文件。 Add a line to your script:在脚本中添加一行:

import os
print(os.getcwd())

then check if wifipasswords.txt is in that directory (it should be).然后检查wifipasswords.txt是否在该目录中(应该是)。 If you want to explicitly place the file in the same directory as the script (not a good idea in general since scripts are often installed in protected locations, but okay for personal use), you can explicitly change the working directory with something like:如果您想将文件显式放置在与脚本相同的目录中(通常不是一个好主意,因为脚本通常安装在受保护的位置,但可以供个人使用),您可以使用以下内容显式更改工作目录:

import os
import os.path

os.chdir(os.path.dirname(__file__))

or explicitly qualify the file name without changing your working directory, eg:或在不更改工作目录的情况下明确限定文件名,例如:

path = os.path.join(os.path.dirname(__file__), 'wifipasswords.txt')

暂无
暂无

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

相关问题 当我双击 .py 文件时 GUI 会打开,但当我在 pycharm 中运行它时不会打开 - GUI opens when i double cklick the .py file, but doesn't when i run it in pycharm 当我从批处理文件运行 .py 文件时打开 PyCharm - PyCharm opening when I run .py file from batch file Python:如何让脚本在运行时写入文件,然后保持打开状态以在按键上进行写入? - Python: How can I have a script write to a file when it's run then stay open to write on a keypress? 我有两个(.py)文件。 当第一个程序结束时,它将自行关闭,然后打开并运行第二个程序文件。 我该怎么做? - I have got two (.py) files. When the first program comes to end, it will close itself then open and run the second program file. How can ı do it? 当运行 docker-compose up 我得到 python: can't open file 'manage.py': [Errno 2] No such file or directory - When run docker-compose up I get python: can't open file 'manage.py': [Errno 2] No such file or directory 当我尝试在批处理文件中使用python脚本写入文件然后运行它时。 它不会写入文件 - When I try to write to a file using python script in a batch file and then run it. It doesn't write to the file 当我尝试在终端中运行 .py 文件时,它不起作用 - When I try run a .py file in my terminal it doesn’t work 当我尝试运行一个py文件时出现Python错误 - Python error when I try to run one py file 我无法运行 this.py 文件。 收到很多错误。 无法弄清楚发生了什么 - I am unable to run this .py file. Recieved many errors. Couldn't figure out what's happeneing 当我尝试运行SendKeys.py时,Python无法识别_sendkeys.c文件 - Python isn't recognizing _sendkeys.c file when I try to run SendKeys.py
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM