Why os.path.abspath () didn't get the real path

Why do I always get paths like "workspace" + "filename" when I use os.path.abspath () to get the absolute path to a file?

import os

dir = "E:/Videos"

for f in os.listdir(dir):
    print(os.path.abspath(f))

output is:

Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
==================== RESTART: E:/Workspace/Python/Test.py ====================
E:\Workspace\Python\13.gif
E:\Workspace\Python\b0020dgz88t.p401.1.mp4
E:\Workspace\Python\c0021dj07u4.p401.1.mp4
E:\Workspace\Python\n0021jtr5c6.p401.1.mp4
E:\Workspace\Python\o0019ba3fy0.p401.1.mp4
E:\Workspace\Python\x0021k863rf.p401.1.mp4
E:\Workspace\Python\z0015vpyqkv.p402.1.mp4
>>> 
Mar.03,2021

the problem lies in your understanding of os. listdir () works by listing all the file names under a certain path. Note that it is file name , not a file entity, which is very important. The function of os.path.abspath () is to add the absolute path of the current working directory to a given file name. As for whether your given file name corresponds to a file that really exists (because you may want to create a file), os.listdir () returns the name of each file in the directory. This will give you:

xxx.mp4
yyy.mp4
...

when you pass these to os.path.abspath (), they are treated as relative paths. This means that it is relative to the directory where you are executing the code. That's why you want to "E:WorkspacePythonxxx.mp4".

as in your question, just connect the file name with the directory path being listed:

import os
dir = "E:/Videos"
for f in os.listdir(dir):
    print(os.path.abspath(os.path.join(dir, f)))

os.path.abspath simply delete things like . and .. and provide the full path from the root of the current directory to the file name (or symlink). If you give an absolute path (starting with a drive letter under windows and / under * nix) and there is no . or .. , then os.path.abspath does nothing.

reference:
https://stackoverflow.com/a/2.


listdir fetches only the files / files under the file folder, not the complete path, while python only knows the current work path (work directory), so enter it as you can see. You can os.chdir (directory) to relocate the work path, so that you can type out


as you want.

you need to redirect to the f directory

first.
    import os
    -sharp ur codes object f
    f_path = os.chdir(f)
    -sharp now get ur abs path 
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b369de-2c02f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b369de-2c02f.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?