Home > other >  How can I get the actual absolute path of a file?
How can I get the actual absolute path of a file?

Time:08-21

I need to get the absolute path of a file in python, i already tried os.path.abspath(filename) in my code like this:

def encrypt(filename):
   with open(filename, 'rb') as toencrypt:
      content = toencrypt.read()
      content = Fernet(key).encrypt(content)
   with open(filename, "wb") as toencrypt:
      toencrypt.write(content)

def checkfolder(folder):
   for file in os.listdir(folder):
      fullpath = os.path.abspath(file)
      if file != "decrypt.py" and file != "encrypt.py" and file != "key.txt" and not os.path.isdir(file):
         print(fullpath)
         encrypt(fullpath)
      elif os.path.isdir(file) and not file.startswith("."):
         checkfolder(fullpath)

the problem is that os.path.abspath(filename) doesn't really get the absolute path, it just attach the current working directory to the filename which in this case is completely useless. how can i do this?

CodePudding user response:

I believe you can use: os.path.join(folder, file)

CodePudding user response:

You can use os.path.join(folder, folder, file)

Refer Link for your more knowledge

  • Related