This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 5 years ago.
How to get ONLY filename instead of full path?
For example:
path = /folder/file.txt
and i need to get:
filename = file.txt
How to do that?
You should use the os module:
import os
filename = os.path.basename(path)
For other path manipulations look here (for python 2.7) or here (for python 3)
Related
This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 2 years ago.
so there are alot of files with .lnk extension in the start menu folder C:\ProgramData\Microsoft\Windows\Start Menu\Programs i want to print all those file names so i tried this code:
import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive = True):
print(file)
it prints the link to the files, but i want to print only the file names with the extension of ".lnk"
Convert the absolute path to list then take the last element from the list. See the below code.
import os
import glob
startmenu = r'C:\ProgramData\Microsoft\Windows\Start Menu\Programs'
os.chdir(startmenu)
for file in glob.glob("**/*.lnk", recursive=True):
print(os.path.split(file)[-1])
This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 2 years ago.
I was trying to extract the file which contains EOB_FILE
for example I have something like
s = "path Omega/CC/Pune/SYNTT/EOB_PROCESSED_BY_OCR/EOB_FILE/0A225618045646F2AEEFC23E74CAC253/0A225618045646F2AEEFC23E74CAC253_page1.json"
How can I get only the file name which is 0A225618045646F2AEEFC23E74CAC253_page1.json
Code I tried :
val = re.findall(r'([^.]*EOB_FILE[^.]*)', s)
val
['path Omega/CC/Pune/SYNTT/EOB_PROCESSED_BY_OCR/EOB_FILE/0A225618045646F2AEEFC23E74CAC253/0A225618045646F2AEEFC23E74CAC253_page1']
Output expected :
0A225618045646F2AEEFC23E74CAC253_page1.json
you can use pathlib.Path:
from pathlib import Path
Path(s).name
output:
'0A225618045646F2AEEFC23E74CAC253_page1.json'
to check if EOB_FILE is in the path you could use:
'EOB_FILE' in Path(s).parts
or:
'EOB_FILE' in s
if 'EOB_FILE' in s:
val = Path(s).name
import os
s = "path Omega/CC/Pune/SYNTT/EOB_PROCESSED_BY_OCR/EOB_FILE/0A225618045646F2AEEFC23E74CAC253/0A225618045646F2AEEFC23E74CAC253_page1.json"
os.path.basename(s)
os is python miscellaneous operating system interfaces. Check documentation here
This question already has answers here:
How do I get the path and name of the file that is currently executing?
(26 answers)
Closed 3 years ago.
So I have a python file sample.py which is in let's say 'A/B/C' and my current working directory is let's say 'D/E'. Now in sample.py I am needed it's directory that is 'A/B/C' . Can anyone help me with this. I have tried "dir_path = os.path.dirname(os.path.realpath('__sample__'))" but it returns the current working directory.
Change the line:
dir_path = os.path.dirname(os.path.realpath('__sample__'))
to
dir_path = os.path.dirname(os.path.realpath(__file__))
This should work for you. Find more about __file__ here.
This question already has answers here:
Extract file name from path, no matter what the os/path format
(22 answers)
Closed 6 years ago.
I have a variable called dllName that grabs the name of a dll that has been executed. Sometimes this dll is returned in the format of "kernel32.dll" and sometimes as "C:\Windows\system32\kernel32.dll".
The path can vary, what I am trying to achieve is the stripping of the "C:\Windows\system32\".
EDIT: Extract file name from path, no matter what the os/path format
My question is not the same as this question, as os.path.basename and os.path.split do not work in this situation.
For os.path.split the head is empty and the tail contains the whole path?
You could use :
path = 'C:\\Windows\\system32\\kernel32.dll'
print path.split('\\')[-1]
#=> kernel32.dll
or
import os.path
print os.path.basename(path)
or
import re
def extract_basename(path):
"""Extracts basename of a given path. Should Work with any OS Path on any OS"""
basename = re.search(r'[^\\/]+(?=[\\/]?$)', path)
if basename:
return basename.group(0)
print extract_basename(path)
This last example should work for any OS, any Path.
Here are some tests.
This question already has answers here:
How do I get the filename without the extension from a path in Python?
(31 answers)
Closed 8 years ago.
For example if I have the variable "file.txt", I want to be able to save only "file" to a variable. What I'd like is to eliminate whatever is beyond the last dot (including the dot). So if i had "file.version2.txt", I'd be left with "file.version2".
Is there a way to do this?
you have to use os.path.splitext
In [3]: os.path.splitext('test.test.txt')
Out[3]: ('test.test', '.txt')
In [4]: os.path.splitext('test.test.txt')[0]
Out[4]: 'test.test'
full reference for similar manipulations can be found here http://docs.python.org/2/library/os.path.html
Using the module os.path you can retrieve the full name of the file and then remove the extension:
import os
file_name, file_ext = os.path.splitext(os.path.basename(path_to_your_file))
If this is not too long you can do something like this if the file is in same directory
old_f = 'file.version2.txt'
new_f = old_f.split('.')
sep = '.'
sep.join(new_f[:-1]) # or assign it to a variable current_f = sep.join(new_f[:-1])