Python 3.7.4函数open()不在pycharm中打开文件

Python 3.7.4函数open()不在pycharm中打开文件,pycharm,python-3.7,Pycharm,Python 3.7,环境:Windows 10 我工作的程序:Pycharm最新版本 如果我写这段代码: open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt", "r") 但是如果我运行代码,什么都不会发生。 我试着用同样的方法把文件放在这里,但什么也没发生 open("testopenfile.txt", "r") 没有语法错误。just:进程已完成,退出代码为0 但没有文件游戏。 我在另一台电脑上试过。同样的结果 我试着用一个不同的文本文件

环境:Windows 10 我工作的程序:Pycharm最新版本

如果我写这段代码:

open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt", "r")
但是如果我运行代码,什么都不会发生。 我试着用同样的方法把文件放在这里,但什么也没发生

open("testopenfile.txt", "r")
没有语法错误。just:进程已完成,退出代码为0 但没有文件游戏。 我在另一台电脑上试过。同样的结果 我试着用一个不同的文本文件,甚至是一张.jpg的图片,什么都没发生。 我做错了什么? 在使用此功能之前,是否需要导入模块:

open()
在我浏览的每一个网站上,这都是做到这一点的方法。 在我尝试此操作的其中一台计算机上,我从windows defender收到一个错误。 然后我就关掉了整个防火墙。没有结果。 我是该目录的完全管理员

我试图通过将文件放在其他目录中来简化, 但它也不起作用

open("L:\\Python\\testopenfile.txt")
open("L:\\testopenfile.txt")
为了测试我是否真的拥有适当的权限,我尝试创建一个文件:

f = open("myfile.txt", "x")
文件立即在pycharm项目目录中创建

解决方案:

# Read from .txt file whithin Python command line interface
# Files can be opened with the open statement.
file = open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt","r")
contentfile = file.read()
print(contentfile)
# Files should always be closed after use.
file.close()

# Open files via standard programm Like files with extension .txt via 
notepad, .jpg via windows fotos.
import webbrowser
webbrowser.open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt","r")
webbrowser.open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt","r")

感谢您的批评:

# Read from .txt file whithin Python command line interface
# Files can be opened with the open statement.
file = open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt","r")
contentfile = file.read()
print(contentfile)
# Files should always be closed after use.
file.close()

# Open files via standard programm Like files with extension .txt via 
notepad, .jpg via windows fotos.
import webbrowser
webbrowser.open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt","r")
webbrowser.open("L:\\Python\\Projecten\\windowsfiles\\testopenfile.txt","r")
openL:\\Python\\Projecten\\windowsfiles\\testopenfile.txt,r

没有语法错误。just:进程已完成,退出代码为0

open函数只返回一个file对象。由于您没有对打开的文件执行任何操作,因此代码将完成执行并以状态0结束,这意味着没有错误

要在控制台中查看文件内容,可以调用文件的read方法将其读入内存,然后使用print函数将其发送到标准输出

此外,由于文件对象支持上下文管理协议,因此可以在with语句中使用它们,文件将自动关闭

因此,为了简化您发布的解决方案,您可以:

my_file = 'L:/Python/Projecten/windowsfiles/testopenfile.txt'
with open(my_file) as f:
    print(f.read())

你到底想达到什么目的。如果它只是一个读数,那么它是正确的。如果您想打开文件进行编写,oyu需要将r+而不是仅将ri与pycharm一起使用。我只想打开一个文件来读取它。在本例中是一个.txt文件。但任何文件都不起作用。执行“打开”操作只会创建一个要使用的文件对象。如果你想阅读内容,你需要阅读在打电话打开后你在做什么?请附上一份报告,并解释你预期会发生什么。您是否希望您的文本文件在记事本或其他文件中打开?但是没有文件是什么意思?请快速阅读我在谷歌上找到的第一个教程。我也尝试了你的解决方案,效果很好。谢谢