Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.05; 'failing': 0.05; 'paths': 0.05; '-*-': 0.07; 'assignment': 0.07; 'filenames': 0.07; 'friday,': 0.07; 'skip:o 50': 0.07; 'utf-8': 0.07; 'coding:': 0.09; 'lines:': 0.09; 'name)': 0.09; 'skip:[ 30': 0.09; 'assume': 0.11; 'files.': 0.13; 'result.': 0.15; 'folder,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'item:': 0.16; 'line)': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'paths.': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'wrote:': 0.16; 'directory.': 0.18; 'skip:l 30': 0.18; 'variable': 0.20; 'trying': 0.22; 'assumes': 0.22; 'names.': 0.22; 'pass': 0.22; '2015': 0.23; 'errors': 0.23; 'this:': 0.23; 'tried': 0.24; 'import': 0.24; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'module.': 0.27; "doesn't": 0.28; 'code:': 0.29; 'function': 0.30; 'folder': 0.31; 'fri,': 0.31; 'received:84': 0.32; 'returned': 0.32; 'problem': 0.33; 'point,': 0.33; 'qualify': 0.33; 'file': 0.34; 'add': 0.34; 'wrong': 0.35; 'to:addr:python-list': 0.35; 'files,': 0.35; 'path': 0.35; 'something': 0.35; 'list': 0.35; 'but': 0.36; 'subject:: ': 0.37; '12,': 0.37; "skip:' 20": 0.37; 'item': 0.38; 'names': 0.38; 'files': 0.38; 'mean': 0.38; 'pm,': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'even': 0.61; "you'll": 0.61; 'more': 0.62; 'above,': 0.63; 'land': 0.63; 'complete': 0.63; 'here': 0.66; 'directory:': 0.84; 'qualified:': 0.84; 'skip:/ 30': 0.84; 'hand,': 0.97 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=OoyysHLt c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=QrohdLjRRo4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=ywQh41txflUsSzNlUhsA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 Date: Sat, 13 Jun 2015 19:25:17 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.7.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Path, strings, and lines References: <56ac4185-3d58-473a-ad40-99d9366a8bb3@googlegroups.com> <58a13725-92f7-407b-8444-71080ada1538@googlegroups.com> In-Reply-To: <58a13725-92f7-407b-8444-71080ada1538@googlegroups.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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: 86 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1434219928 news.xs4all.nl 2914 [2001:888:2000:d::a6]:47160 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92593 On 2015-06-13 05:48, Malik Rumi wrote: > On Friday, June 12, 2015 at 3:31:36 PM UTC-5, Ian wrote: >> 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. >> > > Yes, that is what I want. > >> > 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. > > Can you explain a little more what you mean by fileinput lacking the context of s4? > listdir returns the names of the files that are in the folder, not their paths. If you give fileinput only the names of the files, it'll assume they're in the current folder (directory), which they (probably) aren't. You need to give fileinput the complete _paths_ of the files, not just their names. >> >> > 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. > > By 'qualify', do you mean something like > l.append(fileinput.filename())? > >> >> > 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 tried new code: > > # -*- coding: utf-8 -*- > import os > import fileinput > > os.join _returns its result. > os.path.join('/Projects/Pipeline/4 Transforms', '/Projects/P5/shortstories/') > s2 = os.listdir('/Projects/P5/shortstories/') At this point, s2 contains a list of _names_. You pass those names to fileinput.input, but where are they? In which folder? It assumes they're in the current folder (directory), but they're not! > for item in fileinput.input(s2): > if 'penelope' in item: > print(item) > > But still got the same errors even though the assignment of the path variable seems to have worked: > [snip] Try this: filenames = os.listdir('/Projects/P5/shortstories/') paths = [os.join('/Projects/P5/shortstories/', name) for name in names] for item in fileinput.input(paths):