如何在python中读取其他目录中的文件

时间:2023-02-06 10:46:37

I have a file its name is 5_1.txt in a directory I named it direct ,
how can I read that file using the instruction read.

我有一个文件,其名称是5_1.txt在我直接命名的目录中,如何使用读取的指令读取该文件。

i verified the path using :

我用以下方法验证了路径:

import os
os.getcwd()
os.path.exists(direct)

the result was
True

结果是真的

x_file=open(direct,'r')  

and i got this error :

我收到了这个错误:

Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
x_file=open(direct,'r')
IOError: [Errno 13] Permission denied

i don't know why i can't read the file ? any suggestion ?
thanks .

我不知道为什么我看不懂文件?有什么建议吗?谢谢 。

8 个解决方案

#1


13  

Looks like you are trying to open a directory for reading as if it's a regular file. Many OSs won't let you do that. You don't need to anyway, because what you want (judging from your description) is

看起来你正在尝试打开一个目录进行阅读,就像它是一个普通文件一样。许多操作系统都不允许你这样做。你无论如何都不需要,因为你想要的(从你的描述来看)是

x_file = open(os.path.join(direct, "5_1.txt"), "r")  

or simply

或简单地说

x_file = open(direct+"/5_1.txt", "r")

#2


10  

In case you're not in the specified directory (i.e. direct), you should use (in linux):

如果你不在指定的目录(即直接),你应该使用(在Linux中):

x_file = open('path/to/direct/filename.txt')

Note the quotes and the relative path to the directory.

请注意引号和目录的相对路径。

This may be your problem, but you also don't have permission to access that file. Maybe you're trying to open it as another user.

这可能是您的问题,但您也无权访问该文件。也许你正试图以另一个用户的身份打开它。

#3


3  

You can't "open" a directory using the open function. This function is meant to be used to open files.

您无法使用open函数“打开”目录。此函数用于打开文件。

Here, what you want to do is open the file that's in the directory. The first thing you must do is compute this file's path. The os.path.join function will let you do that by joining parts of the path (the directory and the file name):

在这里,您要做的是打开目录中的文件。您必须做的第一件事是计算此文件的路径。 os.path.join函数可以通过连接路径的一部分(目录和文件名)来实现:

fpath = os.path.join(direct, "5_1.txt")

You can then open the file:

然后,您可以打开该文件:

f = open(fpath)

And read its content:

并阅读其内容:

content = f.read()

Additionally, I believe that on Windows, using open on a directory does return a PermissionDenied exception, although that's not really the case.

另外,我相信在Windows上,在目录上使用open会返回PermissionDenied异常,尽管事实并非如此。

#4


2  

i found this way useful also.

我发现这种方式也很有用。

import tkinter.filedialog
from_filename = tkinter.filedialog.askopenfilename()  

here a window will appear so you can browse till you find the file , you click on it then you can continue using open , and read .

这里会出现一个窗口,以便您可以浏览直到找到该文件,单击它然后您可以继续使用打开,然后阅读。

from_file = open(from_filename, 'r')
contents = from_file.read()
contents

#5


0  

As error message said your application has no permissions to read from the directory. It can be the case when you created the directory as one user and run script as another user.

正如错误消息所示,您的应用程序无权从目录中读取。当您将目录创建为一个用户并将脚本作为另一个用户运行时,可能就是这种情况。

#6


0  

For folks like me looking at the accepted answer, and not understanding why it's not working, you need to add quotes around your sub directory, in the green checked example,

对于像我这样看待已接受答案的人,并且不理解为什么它不起作用,你需要在子目录周围添加引号,在绿色检查的例子中,

x_file = open(os.path.join(direct, "5_1.txt"), "r")  

should actually be

应该是

x_file = open(os.path.join('direct', "5_1.txt"), "r")   

#7


0  

For windows you can either use the full path with '\\' ('/' for Linux and Mac) as separator of you can use os.getcwd to get the current working directory and give path in reference to the current working directory

对于Windows,您可以使用带有'\\'的完整路径(对于Linux和Mac的'/')作为分隔符,您可以使用os.getcwd获取当前工作目录并提供引用当前工作目录的路径

data_dir = os.getcwd()+'\\child_directory'
file = open(data_dir+'\\filename.txt', 'r')

When I tried to give the path of child_diectory entirely it resulted in error. For e.g. in this case:

当我试图完全给出child_diectory的路径时,它会导致错误。对于例如在这种情况下:

file = open('child_directory\\filename.txt', 'r')

Resulted in error. But I think it must work or I am doing it somewhat wrong way but it doesn't work for me. The about way always works.

导致错误。但我觉得它必须有效,或者我做得有些错误,但它对我不起作用。约会的方式总是有效的。

#8


-1  

x_file = open(os.path.join(direct, '5_1.txt'), 'r')

x_file = open(os.path.join(direct,'5_1.txt'),'r')

#1


13  

Looks like you are trying to open a directory for reading as if it's a regular file. Many OSs won't let you do that. You don't need to anyway, because what you want (judging from your description) is

看起来你正在尝试打开一个目录进行阅读,就像它是一个普通文件一样。许多操作系统都不允许你这样做。你无论如何都不需要,因为你想要的(从你的描述来看)是

x_file = open(os.path.join(direct, "5_1.txt"), "r")  

or simply

或简单地说

x_file = open(direct+"/5_1.txt", "r")

#2


10  

In case you're not in the specified directory (i.e. direct), you should use (in linux):

如果你不在指定的目录(即直接),你应该使用(在Linux中):

x_file = open('path/to/direct/filename.txt')

Note the quotes and the relative path to the directory.

请注意引号和目录的相对路径。

This may be your problem, but you also don't have permission to access that file. Maybe you're trying to open it as another user.

这可能是您的问题,但您也无权访问该文件。也许你正试图以另一个用户的身份打开它。

#3


3  

You can't "open" a directory using the open function. This function is meant to be used to open files.

您无法使用open函数“打开”目录。此函数用于打开文件。

Here, what you want to do is open the file that's in the directory. The first thing you must do is compute this file's path. The os.path.join function will let you do that by joining parts of the path (the directory and the file name):

在这里,您要做的是打开目录中的文件。您必须做的第一件事是计算此文件的路径。 os.path.join函数可以通过连接路径的一部分(目录和文件名)来实现:

fpath = os.path.join(direct, "5_1.txt")

You can then open the file:

然后,您可以打开该文件:

f = open(fpath)

And read its content:

并阅读其内容:

content = f.read()

Additionally, I believe that on Windows, using open on a directory does return a PermissionDenied exception, although that's not really the case.

另外,我相信在Windows上,在目录上使用open会返回PermissionDenied异常,尽管事实并非如此。

#4


2  

i found this way useful also.

我发现这种方式也很有用。

import tkinter.filedialog
from_filename = tkinter.filedialog.askopenfilename()  

here a window will appear so you can browse till you find the file , you click on it then you can continue using open , and read .

这里会出现一个窗口,以便您可以浏览直到找到该文件,单击它然后您可以继续使用打开,然后阅读。

from_file = open(from_filename, 'r')
contents = from_file.read()
contents

#5


0  

As error message said your application has no permissions to read from the directory. It can be the case when you created the directory as one user and run script as another user.

正如错误消息所示,您的应用程序无权从目录中读取。当您将目录创建为一个用户并将脚本作为另一个用户运行时,可能就是这种情况。

#6


0  

For folks like me looking at the accepted answer, and not understanding why it's not working, you need to add quotes around your sub directory, in the green checked example,

对于像我这样看待已接受答案的人,并且不理解为什么它不起作用,你需要在子目录周围添加引号,在绿色检查的例子中,

x_file = open(os.path.join(direct, "5_1.txt"), "r")  

should actually be

应该是

x_file = open(os.path.join('direct', "5_1.txt"), "r")   

#7


0  

For windows you can either use the full path with '\\' ('/' for Linux and Mac) as separator of you can use os.getcwd to get the current working directory and give path in reference to the current working directory

对于Windows,您可以使用带有'\\'的完整路径(对于Linux和Mac的'/')作为分隔符,您可以使用os.getcwd获取当前工作目录并提供引用当前工作目录的路径

data_dir = os.getcwd()+'\\child_directory'
file = open(data_dir+'\\filename.txt', 'r')

When I tried to give the path of child_diectory entirely it resulted in error. For e.g. in this case:

当我试图完全给出child_diectory的路径时,它会导致错误。对于例如在这种情况下:

file = open('child_directory\\filename.txt', 'r')

Resulted in error. But I think it must work or I am doing it somewhat wrong way but it doesn't work for me. The about way always works.

导致错误。但我觉得它必须有效,或者我做得有些错误,但它对我不起作用。约会的方式总是有效的。

#8


-1  

x_file = open(os.path.join(direct, '5_1.txt'), 'r')

x_file = open(os.path.join(direct,'5_1.txt'),'r')