How to add the filename to the last line read in of every file?How do I check whether a file exists without exceptions?How do I copy a file in Python?How do I include a JavaScript file in another JavaScript file?How do you read from stdin?How do I list all files of a directory?How to read a file line-by-line into a list?Correct way to write line to file?How to read a large file, line by line, in PythonWhy is reading lines from stdin much slower in C++ than Python?How to import an SQL file using the command line in MySQL?
"Oh no!" in Latin
How do I prevent inappropriate ads from appearing in my game?
Is there a reason to prefer HFS+ over APFS for disk images in High Sierra and/or Mojave?
What is the meaning of "You've never met a graph you didn't like?"
How to make money from a browser who sees 5 seconds into the future of any web page?
Do I have to take mana from my deck or hand when tapping a dual land?
Review your own paper in Mathematics
Giving feedback to someone without sounding prejudiced
Make a Bowl of Alphabet Soup
Sound waves in different octaves
Unable to disable Microsoft Store in domain environment
Is there a way to play vibrato on the piano?
Limit max CPU usage SQL SERVER with WSRM
What (the heck) is a Super Worm Equinox Moon?
How to test the sharpness of a knife?
Why didn’t Eve recognize the little cockroach as a living organism?
Is anti-Brahminism a quality of Asuras and Rakshasas?
Identifying "long and narrow" polygons in with PostGIS
Why is the principal energy of an electron lower for excited electrons in a higher energy state?
What is the smallest number n> 5 so that 5 ^ n ends with "3125"?
Why didn't Voldemort know what Grindelwald looked like?
How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?
What happens if I try to grapple an illusory duplicate from the Mirror Image spell?
Echo with obfuscation
How to add the filename to the last line read in of every file?
How do I check whether a file exists without exceptions?How do I copy a file in Python?How do I include a JavaScript file in another JavaScript file?How do you read from stdin?How do I list all files of a directory?How to read a file line-by-line into a list?Correct way to write line to file?How to read a large file, line by line, in PythonWhy is reading lines from stdin much slower in C++ than Python?How to import an SQL file using the command line in MySQL?
I have a set of files that are read in line by line. I would like have the last line of every file to have the name of the file next to it. This is the code that accomplishes the reading in the file part but I don't know how to get the filenames to show up:
import glob
a = []
def convert_txt_to_dataframe(path):
for files in glob.glob(path + "./*manual.txt"):
for x in open(files):
a.append(x)
So this accomplishes importing all the text files line by line, so now I want every the last line of every file to have an accompanying filename next to it
I want it to look something like:
Hello Goodbye
0 Thank you for being a loyal customer. MyDocuments/TextFile1
1 Thank you for being a horrible customer. MyDocuments/TextFile1
2 Thank you for being a nice customer. MyDocuments/TextFile3
python python-3.x dataframe import glob
|
show 2 more comments
I have a set of files that are read in line by line. I would like have the last line of every file to have the name of the file next to it. This is the code that accomplishes the reading in the file part but I don't know how to get the filenames to show up:
import glob
a = []
def convert_txt_to_dataframe(path):
for files in glob.glob(path + "./*manual.txt"):
for x in open(files):
a.append(x)
So this accomplishes importing all the text files line by line, so now I want every the last line of every file to have an accompanying filename next to it
I want it to look something like:
Hello Goodbye
0 Thank you for being a loyal customer. MyDocuments/TextFile1
1 Thank you for being a horrible customer. MyDocuments/TextFile1
2 Thank you for being a nice customer. MyDocuments/TextFile3
python python-3.x dataframe import glob
2
Could you illustrate with an example of what exactly you want to achieve?
– Srini
Mar 7 at 22:04
1
Soa[-1] += files
after thefor x in ..
loop?
– Martijn Pieters♦
Mar 7 at 22:07
@Martihn Pieters, can you please write out the full code so I understand fully what you are saying?
– mkheifetz
Mar 7 at 22:16
@Srini, I added the output that I would like to see
– mkheifetz
Mar 7 at 22:19
I'm sorry but I still can't understand the desired output. What are Hello and Goodbye? Do 0,1,2 represent the last lines of 3 different files?
– Srini
Mar 7 at 22:29
|
show 2 more comments
I have a set of files that are read in line by line. I would like have the last line of every file to have the name of the file next to it. This is the code that accomplishes the reading in the file part but I don't know how to get the filenames to show up:
import glob
a = []
def convert_txt_to_dataframe(path):
for files in glob.glob(path + "./*manual.txt"):
for x in open(files):
a.append(x)
So this accomplishes importing all the text files line by line, so now I want every the last line of every file to have an accompanying filename next to it
I want it to look something like:
Hello Goodbye
0 Thank you for being a loyal customer. MyDocuments/TextFile1
1 Thank you for being a horrible customer. MyDocuments/TextFile1
2 Thank you for being a nice customer. MyDocuments/TextFile3
python python-3.x dataframe import glob
I have a set of files that are read in line by line. I would like have the last line of every file to have the name of the file next to it. This is the code that accomplishes the reading in the file part but I don't know how to get the filenames to show up:
import glob
a = []
def convert_txt_to_dataframe(path):
for files in glob.glob(path + "./*manual.txt"):
for x in open(files):
a.append(x)
So this accomplishes importing all the text files line by line, so now I want every the last line of every file to have an accompanying filename next to it
I want it to look something like:
Hello Goodbye
0 Thank you for being a loyal customer. MyDocuments/TextFile1
1 Thank you for being a horrible customer. MyDocuments/TextFile1
2 Thank you for being a nice customer. MyDocuments/TextFile3
python python-3.x dataframe import glob
python python-3.x dataframe import glob
edited Mar 7 at 22:47
martineau
69.3k1092186
69.3k1092186
asked Mar 7 at 22:03
mkheifetzmkheifetz
186214
186214
2
Could you illustrate with an example of what exactly you want to achieve?
– Srini
Mar 7 at 22:04
1
Soa[-1] += files
after thefor x in ..
loop?
– Martijn Pieters♦
Mar 7 at 22:07
@Martihn Pieters, can you please write out the full code so I understand fully what you are saying?
– mkheifetz
Mar 7 at 22:16
@Srini, I added the output that I would like to see
– mkheifetz
Mar 7 at 22:19
I'm sorry but I still can't understand the desired output. What are Hello and Goodbye? Do 0,1,2 represent the last lines of 3 different files?
– Srini
Mar 7 at 22:29
|
show 2 more comments
2
Could you illustrate with an example of what exactly you want to achieve?
– Srini
Mar 7 at 22:04
1
Soa[-1] += files
after thefor x in ..
loop?
– Martijn Pieters♦
Mar 7 at 22:07
@Martihn Pieters, can you please write out the full code so I understand fully what you are saying?
– mkheifetz
Mar 7 at 22:16
@Srini, I added the output that I would like to see
– mkheifetz
Mar 7 at 22:19
I'm sorry but I still can't understand the desired output. What are Hello and Goodbye? Do 0,1,2 represent the last lines of 3 different files?
– Srini
Mar 7 at 22:29
2
2
Could you illustrate with an example of what exactly you want to achieve?
– Srini
Mar 7 at 22:04
Could you illustrate with an example of what exactly you want to achieve?
– Srini
Mar 7 at 22:04
1
1
So
a[-1] += files
after the for x in ..
loop?– Martijn Pieters♦
Mar 7 at 22:07
So
a[-1] += files
after the for x in ..
loop?– Martijn Pieters♦
Mar 7 at 22:07
@Martihn Pieters, can you please write out the full code so I understand fully what you are saying?
– mkheifetz
Mar 7 at 22:16
@Martihn Pieters, can you please write out the full code so I understand fully what you are saying?
– mkheifetz
Mar 7 at 22:16
@Srini, I added the output that I would like to see
– mkheifetz
Mar 7 at 22:19
@Srini, I added the output that I would like to see
– mkheifetz
Mar 7 at 22:19
I'm sorry but I still can't understand the desired output. What are Hello and Goodbye? Do 0,1,2 represent the last lines of 3 different files?
– Srini
Mar 7 at 22:29
I'm sorry but I still can't understand the desired output. What are Hello and Goodbye? Do 0,1,2 represent the last lines of 3 different files?
– Srini
Mar 7 at 22:29
|
show 2 more comments
3 Answers
3
active
oldest
votes
So I'm assuming you are taking a list of files and those columns you mentioned [0,1,2] are referring to the last lines of each file in your list. With that in mind, I would try a simpler approach instead of a dataframe. And even if you have to use the dataframe for other reasons, perhaps you can convert to text as your last step and try this:
Example File ("ExampleText2"):
I love coffee
I love creamer
I love coffee and creamer
I have a rash..
Code:
last = []
with open('exampleText2.txt', 'r') as f:
last = f.readlines()[-1] + " other FileName"
Output:
last
'I have a rash.. other FileName'
readlines() will return a list of all the lines in your file, so you could try calling the -1 to pull the last line, then add to it.
add a comment |
I'm assuming that the number of lines is more than or equals the number of files.
import glob
words = ['Thank you for being a loyal customer.',
'Thank you for being a horrible customer.',
'Thank you for being a nice customer.']
def convert(path):
a = []
z = 0
for files in glob.glob(path + "/*.txt"):
temp = [words[z],files]
a.append(temp)
z += 1
print (a)
convert(your_path)
add a comment |
The question is ill-defined, but assuming the OP wants the result shown in the DataFrame example (i.e. not just the last line is somehow decorated with the filename, but all lines are), here is a way to achieve that. For this example, we just have two files: file1.txt
contains two lines: 'a' and 'b', file2.txt
contains one line: 'c'.
We write a file reader that returns a list of lists: each sublist contains the filename and a line.
import glob
def get_file(filename):
with open(filename) as f:
return [[filename, line.rstrip('n')] for line in f]
Try it:
m = map(get_file, glob.glob('file*.txt'))
list(m)
Out[]:
[[['file2.txt', 'c']], [['file1.txt', 'a'], ['file1.txt', 'b']]]
Let us flatten these lists to get one two-dimensional array. Also, it is probably nicer to get a result where the files are sorted alphabetically.
def flatten(m):
return [k for sublist in m for k in sublist]
m = map(get_file, sorted(glob.glob('file*.txt')))
flatten(m)
Out[]:
[['file1.txt', 'a'], ['file1.txt', 'b'], ['file2.txt', 'c']]
Now, it sometimes helps to have the line number (say if we are going to put that data in a DataFrame and do further sorting and analytics). Our reader becomes:
def get_file(filename):
with open(filename) as f:
return [[filename, lineno, line.rstrip('n')] for lineno, line in enumerate(f, start=1)]
m = map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
Notice that the map
above lends itself nicely to a multi-threaded reading if we do have a large number of files:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=4) as pool:
m = pool.map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053513%2fhow-to-add-the-filename-to-the-last-line-read-in-of-every-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
So I'm assuming you are taking a list of files and those columns you mentioned [0,1,2] are referring to the last lines of each file in your list. With that in mind, I would try a simpler approach instead of a dataframe. And even if you have to use the dataframe for other reasons, perhaps you can convert to text as your last step and try this:
Example File ("ExampleText2"):
I love coffee
I love creamer
I love coffee and creamer
I have a rash..
Code:
last = []
with open('exampleText2.txt', 'r') as f:
last = f.readlines()[-1] + " other FileName"
Output:
last
'I have a rash.. other FileName'
readlines() will return a list of all the lines in your file, so you could try calling the -1 to pull the last line, then add to it.
add a comment |
So I'm assuming you are taking a list of files and those columns you mentioned [0,1,2] are referring to the last lines of each file in your list. With that in mind, I would try a simpler approach instead of a dataframe. And even if you have to use the dataframe for other reasons, perhaps you can convert to text as your last step and try this:
Example File ("ExampleText2"):
I love coffee
I love creamer
I love coffee and creamer
I have a rash..
Code:
last = []
with open('exampleText2.txt', 'r') as f:
last = f.readlines()[-1] + " other FileName"
Output:
last
'I have a rash.. other FileName'
readlines() will return a list of all the lines in your file, so you could try calling the -1 to pull the last line, then add to it.
add a comment |
So I'm assuming you are taking a list of files and those columns you mentioned [0,1,2] are referring to the last lines of each file in your list. With that in mind, I would try a simpler approach instead of a dataframe. And even if you have to use the dataframe for other reasons, perhaps you can convert to text as your last step and try this:
Example File ("ExampleText2"):
I love coffee
I love creamer
I love coffee and creamer
I have a rash..
Code:
last = []
with open('exampleText2.txt', 'r') as f:
last = f.readlines()[-1] + " other FileName"
Output:
last
'I have a rash.. other FileName'
readlines() will return a list of all the lines in your file, so you could try calling the -1 to pull the last line, then add to it.
So I'm assuming you are taking a list of files and those columns you mentioned [0,1,2] are referring to the last lines of each file in your list. With that in mind, I would try a simpler approach instead of a dataframe. And even if you have to use the dataframe for other reasons, perhaps you can convert to text as your last step and try this:
Example File ("ExampleText2"):
I love coffee
I love creamer
I love coffee and creamer
I have a rash..
Code:
last = []
with open('exampleText2.txt', 'r') as f:
last = f.readlines()[-1] + " other FileName"
Output:
last
'I have a rash.. other FileName'
readlines() will return a list of all the lines in your file, so you could try calling the -1 to pull the last line, then add to it.
answered Mar 7 at 23:26
RockAndRoleCoderRockAndRoleCoder
10516
10516
add a comment |
add a comment |
I'm assuming that the number of lines is more than or equals the number of files.
import glob
words = ['Thank you for being a loyal customer.',
'Thank you for being a horrible customer.',
'Thank you for being a nice customer.']
def convert(path):
a = []
z = 0
for files in glob.glob(path + "/*.txt"):
temp = [words[z],files]
a.append(temp)
z += 1
print (a)
convert(your_path)
add a comment |
I'm assuming that the number of lines is more than or equals the number of files.
import glob
words = ['Thank you for being a loyal customer.',
'Thank you for being a horrible customer.',
'Thank you for being a nice customer.']
def convert(path):
a = []
z = 0
for files in glob.glob(path + "/*.txt"):
temp = [words[z],files]
a.append(temp)
z += 1
print (a)
convert(your_path)
add a comment |
I'm assuming that the number of lines is more than or equals the number of files.
import glob
words = ['Thank you for being a loyal customer.',
'Thank you for being a horrible customer.',
'Thank you for being a nice customer.']
def convert(path):
a = []
z = 0
for files in glob.glob(path + "/*.txt"):
temp = [words[z],files]
a.append(temp)
z += 1
print (a)
convert(your_path)
I'm assuming that the number of lines is more than or equals the number of files.
import glob
words = ['Thank you for being a loyal customer.',
'Thank you for being a horrible customer.',
'Thank you for being a nice customer.']
def convert(path):
a = []
z = 0
for files in glob.glob(path + "/*.txt"):
temp = [words[z],files]
a.append(temp)
z += 1
print (a)
convert(your_path)
answered Mar 8 at 5:09
YusufsnYusufsn
377215
377215
add a comment |
add a comment |
The question is ill-defined, but assuming the OP wants the result shown in the DataFrame example (i.e. not just the last line is somehow decorated with the filename, but all lines are), here is a way to achieve that. For this example, we just have two files: file1.txt
contains two lines: 'a' and 'b', file2.txt
contains one line: 'c'.
We write a file reader that returns a list of lists: each sublist contains the filename and a line.
import glob
def get_file(filename):
with open(filename) as f:
return [[filename, line.rstrip('n')] for line in f]
Try it:
m = map(get_file, glob.glob('file*.txt'))
list(m)
Out[]:
[[['file2.txt', 'c']], [['file1.txt', 'a'], ['file1.txt', 'b']]]
Let us flatten these lists to get one two-dimensional array. Also, it is probably nicer to get a result where the files are sorted alphabetically.
def flatten(m):
return [k for sublist in m for k in sublist]
m = map(get_file, sorted(glob.glob('file*.txt')))
flatten(m)
Out[]:
[['file1.txt', 'a'], ['file1.txt', 'b'], ['file2.txt', 'c']]
Now, it sometimes helps to have the line number (say if we are going to put that data in a DataFrame and do further sorting and analytics). Our reader becomes:
def get_file(filename):
with open(filename) as f:
return [[filename, lineno, line.rstrip('n')] for lineno, line in enumerate(f, start=1)]
m = map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
Notice that the map
above lends itself nicely to a multi-threaded reading if we do have a large number of files:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=4) as pool:
m = pool.map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
add a comment |
The question is ill-defined, but assuming the OP wants the result shown in the DataFrame example (i.e. not just the last line is somehow decorated with the filename, but all lines are), here is a way to achieve that. For this example, we just have two files: file1.txt
contains two lines: 'a' and 'b', file2.txt
contains one line: 'c'.
We write a file reader that returns a list of lists: each sublist contains the filename and a line.
import glob
def get_file(filename):
with open(filename) as f:
return [[filename, line.rstrip('n')] for line in f]
Try it:
m = map(get_file, glob.glob('file*.txt'))
list(m)
Out[]:
[[['file2.txt', 'c']], [['file1.txt', 'a'], ['file1.txt', 'b']]]
Let us flatten these lists to get one two-dimensional array. Also, it is probably nicer to get a result where the files are sorted alphabetically.
def flatten(m):
return [k for sublist in m for k in sublist]
m = map(get_file, sorted(glob.glob('file*.txt')))
flatten(m)
Out[]:
[['file1.txt', 'a'], ['file1.txt', 'b'], ['file2.txt', 'c']]
Now, it sometimes helps to have the line number (say if we are going to put that data in a DataFrame and do further sorting and analytics). Our reader becomes:
def get_file(filename):
with open(filename) as f:
return [[filename, lineno, line.rstrip('n')] for lineno, line in enumerate(f, start=1)]
m = map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
Notice that the map
above lends itself nicely to a multi-threaded reading if we do have a large number of files:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=4) as pool:
m = pool.map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
add a comment |
The question is ill-defined, but assuming the OP wants the result shown in the DataFrame example (i.e. not just the last line is somehow decorated with the filename, but all lines are), here is a way to achieve that. For this example, we just have two files: file1.txt
contains two lines: 'a' and 'b', file2.txt
contains one line: 'c'.
We write a file reader that returns a list of lists: each sublist contains the filename and a line.
import glob
def get_file(filename):
with open(filename) as f:
return [[filename, line.rstrip('n')] for line in f]
Try it:
m = map(get_file, glob.glob('file*.txt'))
list(m)
Out[]:
[[['file2.txt', 'c']], [['file1.txt', 'a'], ['file1.txt', 'b']]]
Let us flatten these lists to get one two-dimensional array. Also, it is probably nicer to get a result where the files are sorted alphabetically.
def flatten(m):
return [k for sublist in m for k in sublist]
m = map(get_file, sorted(glob.glob('file*.txt')))
flatten(m)
Out[]:
[['file1.txt', 'a'], ['file1.txt', 'b'], ['file2.txt', 'c']]
Now, it sometimes helps to have the line number (say if we are going to put that data in a DataFrame and do further sorting and analytics). Our reader becomes:
def get_file(filename):
with open(filename) as f:
return [[filename, lineno, line.rstrip('n')] for lineno, line in enumerate(f, start=1)]
m = map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
Notice that the map
above lends itself nicely to a multi-threaded reading if we do have a large number of files:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=4) as pool:
m = pool.map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
The question is ill-defined, but assuming the OP wants the result shown in the DataFrame example (i.e. not just the last line is somehow decorated with the filename, but all lines are), here is a way to achieve that. For this example, we just have two files: file1.txt
contains two lines: 'a' and 'b', file2.txt
contains one line: 'c'.
We write a file reader that returns a list of lists: each sublist contains the filename and a line.
import glob
def get_file(filename):
with open(filename) as f:
return [[filename, line.rstrip('n')] for line in f]
Try it:
m = map(get_file, glob.glob('file*.txt'))
list(m)
Out[]:
[[['file2.txt', 'c']], [['file1.txt', 'a'], ['file1.txt', 'b']]]
Let us flatten these lists to get one two-dimensional array. Also, it is probably nicer to get a result where the files are sorted alphabetically.
def flatten(m):
return [k for sublist in m for k in sublist]
m = map(get_file, sorted(glob.glob('file*.txt')))
flatten(m)
Out[]:
[['file1.txt', 'a'], ['file1.txt', 'b'], ['file2.txt', 'c']]
Now, it sometimes helps to have the line number (say if we are going to put that data in a DataFrame and do further sorting and analytics). Our reader becomes:
def get_file(filename):
with open(filename) as f:
return [[filename, lineno, line.rstrip('n')] for lineno, line in enumerate(f, start=1)]
m = map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
Notice that the map
above lends itself nicely to a multi-threaded reading if we do have a large number of files:
from concurrent.futures import ThreadPoolExecutor
with ThreadPoolExecutor(max_workers=4) as pool:
m = pool.map(get_file, sorted(glob.glob('file*.txt')))
out = pd.DataFrame(flatten(m), columns=['filename', 'lineno', 'line'])
out
Out[]:
filename lineno line
0 file1.txt 1 a
1 file1.txt 2 b
2 file2.txt 1 c
answered Mar 10 at 15:21
Pierre DPierre D
4,45242746
4,45242746
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55053513%2fhow-to-add-the-filename-to-the-last-line-read-in-of-every-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Could you illustrate with an example of what exactly you want to achieve?
– Srini
Mar 7 at 22:04
1
So
a[-1] += files
after thefor x in ..
loop?– Martijn Pieters♦
Mar 7 at 22:07
@Martihn Pieters, can you please write out the full code so I understand fully what you are saying?
– mkheifetz
Mar 7 at 22:16
@Srini, I added the output that I would like to see
– mkheifetz
Mar 7 at 22:19
I'm sorry but I still can't understand the desired output. What are Hello and Goodbye? Do 0,1,2 represent the last lines of 3 different files?
– Srini
Mar 7 at 22:29