I have this command on linux which I have problem converting into type on Windows:
row = run('cat '+'C:/Users/Kyle/Documents/final/VocabCorpus.txt'+" | wc -l").split()[0]
For the statement " wc - l" is for the line count to see how many lines exist. If I were to change it to the following using "type" command, what should it be?
I tried this and it doesnt work.
row = run('type '+'C:/Users/Kyle/Documents/final/VocabCorpus.txt'+" | wc -l").split()[0]
The run command is below:
def run(command):
output = subprocess.check_output(command, shell=True)
return output
Please help me. Thank you.
You're trying to count the number of lines in a file? Why can't you do that in pure python?
Something like this?
with open('C:/Users/Kyle/Documents/final/VocabCorpus.txt') as f:
row = len(f.readlines())
Actually wc counts \n symbols in your file (proof). If you have big files and want to save some memory, you'd better read it by chunks to have O(1) memory consumption:
CHUNK_SIZE = 4096
def wc_l(filepath):
nlines = 0
with open(filepath, 'rb') as f:
while True:
chunk = f.read(CHUNK_SIZE)
if not chunk:
break
nlines += sum(1 for char in chunks if char == '\n')
return nlines