Python Forum
[fixed] distinguish file name from the path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[fixed] distinguish file name from the path
#1
Hi,

I'm digging into the os.path lib to find a very simple way to get the path without the file name., but with no sucess (I'm missing something)
Basic example: i've the '/X/Y/Z/file.txt' and i need to get '/X/Y/Z'
(opposite of os.path.joint)

Thanks

Paul
Reply
#2
Use os.path.split()
>>> import os
>>> os.path.split('/X/Y/Z/file.txt')
('/X/Y/Z', 'file.txt')
>>> 
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Hi,

os.path is kind of legacy, the pathlib is the current state for dealing with pathes:

>>> from pathlib import Path
>>> p = Path('/X/Y/Z/file.txt')
>>> p
WindowsPath('/X/Y/Z/file.txt')
>>> p.parents[0]
WindowsPath('/X/Y/Z')
>>> p.name
'file.txt'
>>> p.parts
('\\', 'X', 'Y', 'Z', 'file.txt')
>>>
Regards, noisefloor
Reply
#4
I agree with noisefloor, pathlib is superior for paths.

from pathlib import Path

# https://docs.python.org/3/library/pathlib.html

path2files = Path('/home/peterr/temp') 
filelist = [filename for filename in path2files.iterdir() if filename.is_file()]
for filename in filelist:
    print(f"\nfilename: {filename.name}")
    print(f"file suffix: {filename.suffix}")
    print(f"full path: {filename.resolve()}")
    print(f"filepath parts: {filename.parts}")
Long old output, here just 1 part of it:

Output:
filename: vivelafrance.txt file suffix: .txt full path: /home/peterr/temp/vivelafrance.txt filepath parts: ('/', 'home', 'peterr', 'temp', 'vivelafrance.txt')
Reply
#5
thanks to all contributors
Reply
#6
Old way to get the dirname:

import os


dirname = os.path.dirname(r"/home/user/.local/directory/some_file.txt")
print(dirname)
Output:
/home/user/.local/directory
But this should not be used directly. Use Path instead. The pathlib uses the os.path module.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  re does not seem to distinguish \d and \w properly Pedroski55 4 585 Oct-23-2025, 10:46 PM
Last Post: Pedroski55
  import a function from another file using relative path paul18fr 6 10,447 Aug-01-2024, 06:40 AM
Last Post: paul18fr
  File loop curiously skipping files - FIXED mbk34 10 4,399 Feb-10-2024, 07:08 AM
Last Post: buran
  File path by adding various variables Mishal0488 2 6,594 Apr-28-2023, 07:17 PM
Last Post: deanhystad
  Script File Failure-Path Error? jerryf 13 13,578 Nov-30-2022, 09:58 AM
Last Post: jerryf
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 4,149 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  Subprocess.Popen() not working when reading file path from csv file herwin 13 31,099 May-07-2021, 03:26 PM
Last Post: herwin
  Add file to sys.path permanently hcccs 5 19,408 Jan-31-2021, 11:26 AM
Last Post: hcccs
  Referencing a fixed cell Mark17 2 3,258 Dec-17-2020, 07:14 PM
Last Post: Mark17
  PyDrive download file path MiniMinnow 0 4,878 Apr-28-2020, 03:01 PM
Last Post: MiniMinnow

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020