file io - Python glob gives no result -


i have directory contains lot of .csv files, , trying write script runs on files in directory while doing following operation:

remove first , last lines csv files

i running following code:

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

and run script using python3 script.py. though don't error, don't output file either.

there multiple issues in code. first of count number of lines on filename instead of file-object. second problem initialize i=0 , compare against never changes.

personally convert file list of "lines", cut off first , last , write of them new file:

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

using with open allows omit close calls (because done implicitly) if exception occurs.


in case still gives no output print statement shows file being processed:

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

since you're using python-3.5 use pathlib:

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

as jon clements pointed out there better way [1:-1] exclude first , last line using generator function. way reduce amount of memory used , might improve overall performance. example use:

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

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -