Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算Mac与Windows Python的文件数_Python_Windows_Macos_Glob - Fatal编程技术网

计算Mac与Windows Python的文件数

计算Mac与Windows Python的文件数,python,windows,macos,glob,Python,Windows,Macos,Glob,我有一个代码,用于计算目录中的文件数,并从.txt文件中读取行数 该代码在我的Mac电脑上运行良好,但在Windows上不起作用(即使我更改了路径)。这是Windows上的代码: import glob path = 'E:\calpy_em27_neu\spectra_out_demo\140803\*' files = glob.glob(path) with open('info.txt', 'rt') as infofile: for count, line in enumer

我有一个代码,用于计算目录中的文件数,并从.txt文件中读取行数

该代码在我的Mac电脑上运行良好,但在Windows上不起作用(即使我更改了路径)。这是Windows上的代码:

import glob
path = 'E:\calpy_em27_neu\spectra_out_demo\140803\*'
files = glob.glob(path)

with open('info.txt', 'rt') as infofile:
    for count, line in enumerate(infofile):
        print count
print(len(files))
在windows上,我得到输出:

0
1
1
3
0
在Mac上,我得到(正确的)输出:


因为在infofile中有四行,在名为的目录中有四个文件。你知道为什么这不能在Windows上正常工作吗?

也许你需要转义反斜杠字符。请尝试以下方法之一:

path = r'E:\calpy_em27_neu\spectra_out_demo\140803\*'
path = 'E:\\calpy_em27_neu\\spectra_out_demo\\140803\\*'
path = 'E:/calpy_em27_neu/spectra_out_demo/140803/*'

也许您需要转义反斜杠字符。请尝试以下方法之一:

path = r'E:\calpy_em27_neu\spectra_out_demo\140803\*'
path = 'E:\\calpy_em27_neu\\spectra_out_demo\\140803\\*'
path = 'E:/calpy_em27_neu/spectra_out_demo/140803/*'

Windows输出的第三行上的
1
实际上是您得到的,还是输入错误?因为您显示的代码不可能发出
1
。Windows输出的第三行上的
1
实际上就是您得到的,还是输入错误?因为您显示的代码不可能发出那种
1
。是的,第一个代码立即起作用!你知道这其中的原因吗?@alli:“因为它在语言中”。如果你有好奇的头脑,也可以尝试其他两种方法。是的,第一种方法很快就奏效了!你知道这其中的原因吗?@alli:“因为它在语言中”。如果你有好奇的头脑,也可以尝试其他两种。