【发布时间】:2018-09-02 23:32:40
【问题描述】:
这是我关于 SO 的第一个问题 :D 我目前正在编写一些代码。问题是,以下代码在 Pycharm 和 Ubuntu 中有效……但在 Windows 中无效。
import os, os.path
import configparser
if os.path.exists('config') and os.stat("config/config.ini").st_size>0:
#do something
else:
os.makedirs('config')
password = input("Password: ")
ip = input("IP Server: ")
clientname = input("(choosen) Clientname: ")
config = configparser.ConfigParser()
config['DEFAULT'] = {'User': user, 'ServerIP': ip, 'Clientname': clientname}
with open('config/config.ini', 'w+') as configfile:
config.write(configfile)
Windows 错误[WinError2] 找不到文件 config/config.ini
另一方面,Pycharm 和 Ubuntu 是否做了他们必须做的事情。 有谁知道怎么回事?
【问题讨论】:
-
某处可能有一个空的
config目录(当前目录因运行方式而异)。使用os.path.getsize()避免调用stat。您的程序可能在第二部分崩溃,导致config目录为空。 -
脚本文件夹为空。但是您的解决方案有效。非常感谢!
-
您为什么将 Windows 和 Ubuntu 与 PyCharm 进行比较?前者是操作系统,PyCharm 是 Python IDE。是否使用 PyCharm 没有任何区别。
-
@abccd 它在 Pycharm 控制台中工作。但它在 Windows 上的 python shell 中不起作用。但它可以在 ubuntu 上的 python shell 中运行
标签: python python-3.x operating-system pycharm