代码之家  ›  专栏  ›  技术社区  ›  dvd

Python glob没有给出结果

  •  1
  • dvd  · 技术社区  · 7 年前

    我有一个目录,里面有很多 .csv

    从所有csv文件中删除第一行和最后一行

    我正在运行以下代码:

    import glob
    
    list_of_files = glob.glob('path/to/directory/*.csv')
    for file_name in list_of_files:
        fi = open(file_name, 'r')
        fo = open(file_name.replace('csv', 'out'), 'w')  #make new output file for each file
        num_of_lines = file_name.read().count('\n')
        file_name.seek(0)
        i = 0
        for line in fi:
            if i != 1 and i != num_of_lines-1:
                fo.write(line)
    
        fi.close()
        fo.close()
    

    我使用 python3 script.py . 虽然我没有收到任何错误,但我也没有收到任何输出文件。

    1 回复  |  直到 7 年前
        1
  •  2
  •   MSeifert    7 年前

    您的代码中存在多个问题。首先,您要计算 文件名 而不是文件对象。第二个问题是初始化 i=0

    就我个人而言,我只是将文件转换为一个“行”列表,剪掉第一行和最后一行,并将它们全部写入新文件:

    import glob
    
    list_of_files = glob.glob('path/to/directory/*.csv')
    for file_name in list_of_files:
        with open(file_name, 'r') as fi:
            with open(file_name.replace('csv', 'out'), 'w') as fo:
                for line in list(fi)[1:-1]:  # for all lines except the first and last
                    fo.write(line)
    

    with open 允许省略 close 调用(因为它们是隐式完成的),即使发生异常。


    如果仍然没有输出,您可以 print 显示正在处理的文件的语句:

    print(file_name)  # just inside the for-loop before any `open` calls.
    

    因为您使用的是python-3.5,所以也可以使用 pathlib :

    import pathlib
    
    path = pathlib.Path('path/to/directory/')
    
    # make sure it's a valid directory
    assert path.is_dir(), "{} is not a valid directory".format(p.absolute())
    
    for file_name in path.glob('*.csv'):
        with file_name.open('r') as fi:
            with pathlib.Path(str(file_name).replace('.csv', '.out')).open('w') as fo:
                for line in list(fi)[1:-1]:  # for all lines except the first and last
                    fo.write(line)
    

    [1:-1]

    import pathlib
    
    def ignore_first_and_last(it):
        it = iter(it)
        firstline = next(it)
        lastline = next(it)
        for nxtline in it:
            yield lastline
            lastline = nxtline
    
    path = pathlib.Path('path/to/directory/')
    
    # make sure it's a valid directory
    assert path.is_dir(), "{} is not a valid directory".format(p.absolute())
    
    for file_name in path.glob('*.csv'):
        with file_name.open('r') as fi:
            with pathlib.Path(str(file_name).replace('.csv', '.out')).open('w') as fo:
                for line in ignore_first_and_last(fi):  # for all lines except the first and last
                    fo.write(line)