Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92551
| References | <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2015-06-12 14:30 -0600 |
| Subject | Re: Path, strings, and lines |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.438.1434141083.13271.python-list@python.org> (permalink) |
On Fri, Jun 12, 2015 at 1:39 PM, Malik Rumi <malik.a.rumi@gmail.com> wrote:
> I am trying to find a list of strings in a directory of files. Here is my code:
>
> # -*- coding: utf-8 -*-
> import os
> import fileinput
>
> s2 = os.listdir('/home/malikarumi/Projects/P5/shortstories')
Note that the filenames that will be returned here are not fully
qualified: you'll just get filename.txt, not
/home/.../shortstories/filename.txt.
> for line in lines:
> for item in fileinput.input(s2):
fileinput doesn't have the context of the directory that you listed
above, so it's just going to look in the current directory.
> if line in item:
> with open(line + '_list', 'a+') as l:
> l.append(filename(), filelineno(), line)
Although it's not the problem at hand, I think you'll find that you
need to qualify the filename() and filelineno() function calls with
the fileinput module.
> FileNotFoundError: [Errno 2] No such file or directory: 'THE LAND OF LOST TOYS~'
And here you can see that it's failing to find the file because it's
looking in the wrong directory. You can use the os.path.join function
to add the proper directory path to the filenames that you pass to
fileinput.
> I don't know what the tilde at the end of 'The Land of Lost Toys' is about.
The trailing ~ is a convention used by Emacs (and possibly other
editors) for files that it creates as backups.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 12:39 -0700
Re: Path, strings, and lines Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-12 14:30 -0600
Re: Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 21:48 -0700
Re: Path, strings, and lines MRAB <python@mrabarnett.plus.com> - 2015-06-13 19:25 +0100
Struggling with os.path.join and fileinput (was 'Path, strings, and lines' Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-15 19:00 -0700
Re: Struggling with os.path.join and fileinput (was 'Path, strings, and lines' Ian Kelly <ian.g.kelly@gmail.com> - 2015-06-15 20:44 -0600
Re: Struggling with os.path.join and fileinput (was 'Path, strings, and lines' MRAB <python@mrabarnett.plus.com> - 2015-06-16 03:51 +0100
Re: Path, strings, and lines Chris Angelico <rosuav@gmail.com> - 2015-06-13 09:47 +1000
Re: Path, strings, and lines Malik Rumi <malik.a.rumi@gmail.com> - 2015-06-12 21:50 -0700
csiph-web