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? > > > 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.path.join('/Projects/Pipeline/4 Transforms', '/Projects/P5/shortstories/') s2 = os.listdir('/Projects/P5/shortstories/') 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: In [51]: import os In [52]: path = os.path.join('/Projects/Pipeline/4 Transforms', '/Projects/P5/shortstories') In [53]: print(path) /Projects/P5/shortstories In [54]: %run algo_f3.py --------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) /home/malikarumi/Projects/Pipeline/4 Transforms/algo_f3.py in <module>() 5 6 os.path.join('/Projects/Pipeline/4 Transforms', '/Projects/P5/shortstories') ----> 7 s2 = os.listdir('/Projects/P5/shortstories') 8 for item in fileinput.input(s2): 9 if 'penelope' in item: FileNotFoundError: [Errno 2] No such file or directory: '/Projects/P5/shortstories' In [55]: os.listdir(path) --------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) <ipython-input-55-9f0bcdc47648> in <module>() ----> 1 os.listdir(path) FileNotFoundError: [Errno 2] No such file or directory: '/Projects/P5/shortstories' In [56]: %run algo_f4.py --------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) /home/malikarumi/Projects/Pipeline/4 Transforms/algo_f4.py in <module>() 5 6 os.path.join('/Projects/Pipeline/4 Transforms', '/Projects/P5/shortstories/') ----> 7 s2 = os.listdir('/Projects/P5/shortstories/') 8 for item in fileinput.input(s2): 9 if 'penelope' in item: FileNotFoundError: [Errno 2] No such file or directory: '/Projects/P5/shortstories/' Clearly, I don't understand os.path.join() although I have read about it and seen examples. In both https://www.youtube.com/watch?v=t5uRlE28F54 Python path.join, and listdir and in https://courses.cs.washington.edu/courses/cse140/13wi/file-interaction.html they are using a real, existing path to 'create' a path with os.join.path. So my immediate problem is that I don't see why that is necessary if they are dealing with an actual path. Why can't they just use the actual path? This is why I thought putting the absolute path in my code would solve this problem, but obviously not. Can you help me get a better grasp of how to use os.path.join, and why my refactored code still does not work? thanks. > > > 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. -- https://mail.python.org/mailman/listinfo/python-list