glob.glob中的错误,用于Windows中没有扩展的文件 [英] Bug in glob.glob for files w/o extentions in Windows

查看:150
本文介绍了glob.glob中的错误,用于Windows中没有扩展的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows XP上,glob.glob对于没有扩展名的文件无法正常工作。

例如C:\ Temp包含4个文件:2个带扩展,2个没有。


C:\ Temp> dir / b *

aaaaa.aaa

bbbbb.bbb

ccccc

ddddd


C:\ Temp> dir / b * 。

ccccc

ddddd


C:\ Temp> python

Python 2.3(# 46,Jul 29 2003,18:54:32)[MSC v.1200 32 bit(Intel)] on win32

输入help,copyright,credit等等。或许可证或更多信息。

On Windows XP glob.glob doesn''t work properly for files without extensions.
E.g. C:\Temp contains 4 files: 2 with extensions, 2 without.

C:\Temp>dir /b *
aaaaa.aaa
bbbbb.bbb
ccccc
ddddd

C:\Temp>dir /b *.
ccccc
ddddd

C:\Temp>python
Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

import glob
glob.glob(''*'')
[' 'aaaaa.aaa'',''bbbbb.bbb'',''ccccc'',''ddddd'']

glob.glob(''*。'')
import glob glob.glob( ''*'' ) [''aaaaa.aaa'', ''bbbbb.bbb'', ''ccccc'', ''ddddd'']
glob.glob( ''*.'' )



[]


看起来像个bug。


Georgy

-

Georgy Pruss

电子邮件:''ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\ n''。decode(''base6 4'')


[]

It looks like a bug.

Georgy
--
Georgy Pruss
E-mail: ''ZDAwMTEyMHQwMzMwQGhvdG1haWwuY29t\n''.decode(''base6 4'')

推荐答案

好的,你可以称之为不是bug,但是行为不同。

我发现了fnmatch模块就是这个原因。
这里的其他例子:


C:\ temp> dir / b *。*

..eee

aaa.aaa

nnn


C:\ temp> dir / b *#这是*的同义词*。*

..eee

aaa.aaa

nnn


C:\ temp> dir / b。*

..eee


C:\ temp> ; dir / b *。 #它看起来很奇怪

..eee

nnn

C:\ temp> python
OK, you can call it not a bug, but different behavior.
I''ve found that the fnmatch module is the reason for that.
Here''s other examples:

C:\temp>dir /b *.*
..eee
aaa.aaa
nnn

C:\temp>dir /b * # it''s by def synonym for *.*
..eee
aaa.aaa
nnn

C:\temp>dir /b .*
..eee

C:\temp>dir /b *. # it looks strange too
..eee
nnn
C:\temp>python
import glob
glob.glob(''*。*'')
[''aaa.aaa'']

glob.glob(''*'')
[''aaa.aaa'',''nnn'']

glob.glob(''。*'' )
[''.eee'']

glob.glob(''*。'')
import glob glob.glob(''*.*'') [''aaa.aaa'']
glob.glob(''*'') [''aaa.aaa'', ''nnn'']
glob.glob(''.*'') [''.eee'']
glob.glob(''*.'')



[]

似乎在任何情况下我都必须自己提取''nnn'。

类似于:


如果mask.endswith(''。''):#no extention意味着名字中根本没有点

list = glob.glob(mask [: - 1])

list = filter(lambda x:''。''不在x,列表中)#或[x for x in list in if''。''not in x]

else:

list = glob.glob(mask)


G-:


[]
It seems that in any case I''ll have to extract ''nnn'' by myself.
Something like:

if mask.endswith(''.''): # no extention implies actually no dots in name at all
list = glob.glob( mask[:-1] )
list = filter( lambda x: ''.'' not in x, list ) # or [x for x in list if ''.'' not in x]
else:
list = glob.glob( mask )

G-:


在2003年11月30日的太阳报上格林尼治标准时间03:47:38,文章

< news:uL ********************* @ twister.southeast.rr。 com>,Georgy Pruss

写道:
On Sun, 30 Nov 2003 03:47:38 GMT, in article
<news:uL*********************@twister.southeast.rr .com>, Georgy Pruss
wrote:
在Windows XP上,glob.glob对没有扩展名的文件无法正常工作。
例如C:\ Temp包含4个文件:2个带扩展,2个没有。
[...]
C:\ Temp> dir / b *。
ccccc
ddddd


这是标准的Windows行为。它与CP / M兼容,因此对于MS-DOS来说是微不足道的,而且微软在所有版本的

Windows中保留了这种行为。

您是否曾经在FAT分区的目录系统中逛过

(没有VFAT)?你会发现每个文件名都是11个字符

long和。在任何目录中的任何文件名的任何部分都找不到

条目。


这很奇怪,但这就是它的工作方式。如果你试试


dir / b *


cmd.exe只列出没有扩展名的文件吗?
On Windows XP glob.glob doesn''t work properly for files without extensions.
E.g. C:\Temp contains 4 files: 2 with extensions, 2 without.
[...]
C:\Temp>dir /b *.
ccccc
ddddd
This is standard Windows behavior. It''s compatible with CP/M and therefore
MS-DOS, and Microsoft has preserved this behavior in all versions of
Windows.

Did you ever poke around in the directory system in a FAT partition
(without VFAT)? You''ll find that every file name is exactly 11 characters
long and "." is not found in any part of any file name in any directory
entry.

It''s bizarre but that''s the way it works. If you try

dir /b *

does cmd.exe list only files without extensions?
glob.glob(''*。'')
glob.glob( ''*.'' )


[]


[]




glob提供Unix样式路径名模式扩展如

_Python Library Reference_中所述:如果模式中有一个句点(。),那么它必须与文件名中的句号相匹配。

看起来像个bug。



glob provides "Unix style pathname pattern expansion" as documented in the
_Python Library Reference_: If there''s a period (".") in the pattern, it
must match a period in the filename.
It looks like a bug.




不,这是正确的行为。这是'Windows(仍然)搞砸了。



No, it''s proper behavior. It''s Windows that''s (still) screwy.




" Jules Dubois" <博*** @ invalid.tld>在留言新闻中写道:nj ***************************** @ 40tude.net ...

|在Sun,2003年11月30日03:47:38 GMT,在文章

| < news:uL ********************* @ twister.southeast.rr .com> ;, Georgy Pruss

|写道:

|

| >在Windows XP上,glob.glob对于没有扩展名的文件不起作用。

| >例如。 C:\ Temp包含4个文件:2个带扩展名,2个没有。

| > [...]

| > C:\ Temp> dir / b *。

| > ccccc

| > ddddd

|

|这是标准的Windows行为。它与CP / M兼容,因此

| MS-DOS和Microsoft在所有版本的

|中都保留了这种行为Windows。


这就是我的意思,想要和喜欢的。


C''mon伙计们,我不在乎如果它是FAT,NTFS,Windows,Linux,VMS或其他什么。

我想要的只是获取名字中没有点的文件(在我的电脑上:))。

我做到了,如果需要,我可以在任何系统上完成。

|你有没有在FAT分区的目录系统中逛过

| (没有VFAT)?你会发现每个文件名都是11个字符

|长和。在任何目录中的任何文件名的任何部分都找不到

|入场。

|

|这是奇怪的,但这是它的工作方式。如果你试试

|

| dir / b *

|

| cmd.exe是否只列出没有扩展名的文件?


根据定义它与*。*相同,如果我的记忆能为我服务的话。

| >>>> glob.glob(''*。'')

| > []

| >

|

| glob提供Unix样式路径名模式扩展。如

|中所述_Python Library Reference_:如果模式中有句号(。),则

|必须匹配文件名中的句号。

|

| >它看起来像一个bug。

|

|不,这是正确的行为。这是'Windows(仍然)搞砸了。


我明白了。

向全世界展示一个完美的操作系统,你将是一个亿万富翁。


G-:

"Jules Dubois" <bo***@invalid.tld> wrote in message news:nj*****************************@40tude.net...
| On Sun, 30 Nov 2003 03:47:38 GMT, in article
| <news:uL*********************@twister.southeast.rr .com>, Georgy Pruss
| wrote:
|
| > On Windows XP glob.glob doesn''t work properly for files without extensions.
| > E.g. C:\Temp contains 4 files: 2 with extensions, 2 without.
| > [...]
| > C:\Temp>dir /b *.
| > ccccc
| > ddddd
|
| This is standard Windows behavior. It''s compatible with CP/M and therefore
| MS-DOS, and Microsoft has preserved this behavior in all versions of
| Windows.

That''s what I meant, wanted and liked.

C''mon guys, I don''t care if it''s FAT, NTFS, Windows, Linux, VMS or whatever.
All I wanted was to get files w/o dots in their names (on my computer :)).
I did it and I can do it on any system if I need.
| Did you ever poke around in the directory system in a FAT partition
| (without VFAT)? You''ll find that every file name is exactly 11 characters
| long and "." is not found in any part of any file name in any directory
| entry.
|
| It''s bizarre but that''s the way it works. If you try
|
| dir /b *
|
| does cmd.exe list only files without extensions?

By definition it''s the same as *.* if my memory serves me right.
| >>>> glob.glob( ''*.'' )
| > []
| >
|
| glob provides "Unix style pathname pattern expansion" as documented in the
| _Python Library Reference_: If there''s a period (".") in the pattern, it
| must match a period in the filename.
|
| > It looks like a bug.
|
| No, it''s proper behavior. It''s Windows that''s (still) screwy.

I see.
Show the world a perfect OS and you''ll be a billionaire.

G-:


这篇关于glob.glob中的错误,用于Windows中没有扩展的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆