Python 在打开刚刚使用 re.search 找到的文件时不返回此类文件或目录

发布于 2024-12-18 03:31:49 字数 618 浏览 2 评论 0原文

我正在尝试打开许多文件夹中的特定文件,其名称以“.alleles.txt”结尾。我搜索该文件并找到它,但当

它返回: IOError: [Errno 2] No such file or directory: 'abca3.alleles.txt'

我尝试打开它时,

for eachfile in filelisting:                       
    if re.search('\.alleles\.txt$', eachfile):
        allelesfile = open(eachfile, 'r')
        print '2'   

。目录由以下方式指定:

folder = 'E:\\All Data'
folderlisting = os.listdir(folder) 

for eachfolder in folderlisting:
    print eachfolder 
    if os.path.isdir(folder + '\\' + eachfolder):
        filelisting = os.listdir(folder + '\\' + eachfolder)

I am trying to open a specific file in many folders, with the name ending '.alleles.txt'. I search for the file and it is found, but then it returns:

IOError: [Errno 2] No such file or directory: 'abca3.alleles.txt'

when I try and open it.

for eachfile in filelisting:                       
    if re.search('\.alleles\.txt

Directory is specified by:

folder = 'E:\\All Data'
folderlisting = os.listdir(folder) 

for eachfolder in folderlisting:
    print eachfolder 
    if os.path.isdir(folder + '\\' + eachfolder):
        filelisting = os.listdir(folder + '\\' + eachfolder)
, eachfile): allelesfile = open(eachfile, 'r') print '2'

Directory is specified by:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以笙箫默 2024-12-25 03:31:49

您应该使用glob。试试这个

import glob

files = glob.glob('E:\\All Data\\*\\*.alleles.txt')

You should be using glob. Try this

import glob

files = glob.glob('E:\\All Data\\*\\*.alleles.txt')
夏日浅笑〃 2024-12-25 03:31:49

您如何获取文件列表?如果您使用 filelisting = os.walk(directoryname) ,那么它会返回一个特殊的元组。你看过吗?

并且,filelisting 是否包含文件的完整路径?如果文件位于脚本目录以外的目录中,例如存储在 directoryname 变量中,则必须以这种方式打开它们:

allelesfile = open(directoryname + "\\" + eachfile, 'r')

在您输入信息之前,我们无法提供有关您的问题的更多信息这里(实际的filelisting值等)。

How did you obtain the filelisting? If you used filelisting = os.walk(directoryname) then it resturns a special tuple. Did you look at it?

And, does filelisting contain the full path of the files? If the files are in a directory other than the script directory, say it's stored in the directoryname variable, you have to open them this way:

allelesfile = open(directoryname + "\\" + eachfile, 'r')

We can't give more information about your question until you put information here (actual filelisting value, etc).

执着的年纪 2024-12-25 03:31:49

我怀疑您的某些文件名包含非 ASCII 字符。尝试

folder = u'E:\\All Data'
folderlisting = os.listdir(folder)

for eachfolder in folderlisting:
    print eachfolder 
    if os.path.isdir(os.path.join(folder,eachfolder)):
        filelisting = os.listdir(os.path.join(folder,eachfolder))

记下 u'...' 前缀。如果没有它,os.listdir() 可能会默默地删除或 ASCIIfy 文件名中的非 ASCII 字符,这当然会导致无效的文件名。另请参阅此问题

这在os.listdir(path)的文档

在 Windows NT/2k/XP 和 Unix 上,如果 path 是 Unicode 对象,则结果
将是 Unicode 对象的列表。无法解码的文件名仍然是
作为字符串对象返回。

I have a suspicion that some of your filenames contain non-ASCII characters. Try

folder = u'E:\\All Data'
folderlisting = os.listdir(folder)

for eachfolder in folderlisting:
    print eachfolder 
    if os.path.isdir(os.path.join(folder,eachfolder)):
        filelisting = os.listdir(os.path.join(folder,eachfolder))

Note the u'...' prefix. Without it, os.listdir() might silently drop or ASCIIfy non-ASCII characters in your filenames, which then of course leads to invalid filenames. See also this question.

This is stated in the docs for os.listdir(path) :

On Windows NT/2k/XP and Unix, if path is a Unicode object, the result
will be a list of Unicode objects. Undecodable filenames will still be
returned as string objects.

dawn曙光 2024-12-25 03:31:49

出现错误是因为它试图从运行 python 程序的目录中打开文件;而不是文件所在的目录。

您需要为 open 提供文件的完整路径(包括目录名称)。

The error appears because it is trying to open the file from the directory where you are running the python program; and not the directory where the file resides.

You need to give open the full path to your file (including the directory name).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文