Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!2.eu.feeder.erje.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'context': 0.05; 'failing': 0.05; '-*-': 0.07; 'filenames': 0.07; 'skip:o 50': 0.07; 'trailing': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'lines:': 0.09; 'files.': 0.13; 'item:': 0.16; 'line)': 0.16; 'wrote:': 0.16; 'directory.': 0.18; 'creates': 0.18; 'trying': 0.22; 'pass': 0.22; '2015': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'module.': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'about.': 0.29; 'code:': 0.29; 'function': 0.30; 'convention': 0.31; 'fri,': 0.31; 'possibly': 0.32; 'returned': 0.32; 'problem': 0.33; 'qualify': 0.33; 'file': 0.34; 'add': 0.34; 'received:google.com': 0.34; 'wrong': 0.35; 'to:addr:python-list': 0.35; 'path': 0.35; 'list': 0.35; '(and': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'item': 0.38; 'files': 0.38; 'end': 0.39; 'pm,': 0.39; 'to:addr:python.org': 0.39; "you'll": 0.61; 'above,': 0.63; 'land': 0.63; 'here': 0.66; 'directory:': 0.84; 'qualified:': 0.84; 'skip:/ 30': 0.84; 'to:name:python': 0.84; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=UMlFMGgdlktT6bBVlPio89PczI43sWEsjJXl9ktuqD4=; b=hV/k5RBzgC9TZGk6+OCD5rto+RDZZMvYEoyFdH/3d59+PZYLb+vtb1rX5jDrMMTYBO c4UFQrIWny3ZjNPrxIAnGUsecvxCtIljl/hN8hccDaCuM79k9UC174amri2oD/JHr/6w NEubr5tkiQgGwnxVhVlIBriDzTXyb7QcA7tPP+Lzap7wpjRlXQ+PvzFbCdGdeFZTNWwn gw1/jrh3AQ/pjpLM+lvkhQ+FnRpOBbc8xEb6Pqe7lWdY4ZO+FFgO+fr22kOwuINlKLPl qL7CG958Ih5LSWx6pLgsCQ1sgnkYf+5epWP4xV9Dtr9dH8TSeQcvlmL69p0DebEePQN7 Inng== X-Received: by 10.170.69.131 with SMTP id l125mr20733267ykl.39.1434141075005; Fri, 12 Jun 2015 13:31:15 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com> References: <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com> From: Ian Kelly Date: Fri, 12 Jun 2015 14:30:34 -0600 Subject: Re: Path, strings, and lines To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434141083 news.xs4all.nl 2923 [2001:888:2000:d::a6]:54054 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92551 On Fri, Jun 12, 2015 at 1:39 PM, Malik Rumi 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.