1

I was searching how to get the absolute path of a file on python but haven't had much luck. here is my code

import os


directory = raw_input("What's the directory? ")
print "Reading files.."
listing = os.listdir(directory)
for fname in listing:
    with open(fname) as f:
        if "yes" in f.read():
            print f.name
f.close()

my problem is this... the listing is perfectly fine.. but when the listdir method passes the variable to the open method, the variable is passes without the absolute path, so it won't read the files because it is reading a file that has no path.. here is a sample of the error

What's the directory? /home/diego/test Reading files.. Traceback (most recent call last): File "/home/diego/Documents/progra/python/antivirus/antivirus.py", line 14, in with open(fname) as f: IOError: [Errno 2] No such file or directory: 'test'

Can anyone help me with it?

Thanks

Diego Velez
  • 1,584
  • 1
  • 16
  • 20
  • 1
    possible duplicate of [How to get an absolute file path in Python](http://stackoverflow.com/questions/51520/how-to-get-an-absolute-file-path-in-python) – Łukasz Rogalski Apr 28 '15 at 05:28
  • 2
    You probably need to add the directory to each `fname`, like `open(os.path.join(directory, fname))` – Marius Apr 28 '15 at 05:28
  • Not possible duplicate, a duplicate for sure. Use `os.path.abspath("mydir/myfile.txt")` – Tim Biegeleisen Apr 28 '15 at 05:29
  • 2
    I don't think this actually a duplicate, calling `os.path.abspath(fname)` won't help here because using the current code, Python can't find the file because `fname` is not a valid *relative* path. – Marius Apr 28 '15 at 05:31
  • 1
    Relax with the duplications, the absolute part of the question isn't his problem – avim Apr 28 '15 at 05:32
  • @Marius exactly.. fname just contains the name of the file.. not the path, I want to get the path too I can't os.path.abspath method because I'm using variables and it just accepts one – Diego Velez Apr 28 '15 at 05:38

2 Answers2

4
from os.path import abspath

full_path = abspath(filename)
The Unfun Cat
  • 29,987
  • 31
  • 114
  • 156
2

Ok, so your problem hasn't got anything to do with absolute paths. The problem is that you're trying to open a file that exists in another directory using only it's filename. Use os.path.join to create the complete file name and use that:

for fname in listing:
    full_name = os.path.join(directory, fname)
    with open(full_name) as f:
        if "yes" in f.read():
            print f.name
Raniz
  • 10,882
  • 1
  • 32
  • 64
  • the file exists in that directory, I created the file but the path is not passed through the method. [Elementary]$ pwd /home/diego/test [Elementary]$ ls test – Diego Velez Apr 28 '15 at 05:37
  • When you tell python to `open(fname)` it will look for the file in the directory where you're running your script - not the directory you typed into the prompt. That is, instead of trying to open `/home/diego/test/test` it tries to open `/home/diego/Documents/progra/python/antivirus/test` because you only told it to open "test" – Raniz Apr 28 '15 at 05:40
  • 1
    you are right.. and how can I use the path reading my variable "directory" – Diego Velez Apr 28 '15 at 05:42
  • I explained that in my answer: use `os.path.join(directory, fname)` and you'll get "/home/diego/test/test" – Raniz Apr 28 '15 at 05:44