Get Filename Without Extension in Python - python

If I have a filename like one of these:
1.1.1.1.1.jpg
1.1.jpg
1.jpg
How could I get only the filename, without the extension? Would a regex be appropriate?

In most cases, you shouldn't use a regex for that.
os.path.splitext(filename)[0]
This will also handle a filename like .bashrc correctly by keeping the whole name.

>>> import os
>>> os.path.splitext("1.1.1.1.1.jpg")
('1.1.1.1.1', '.jpg')

You can use stem method to get file name.
Here is an example:
from pathlib import Path
p = Path(r"\\some_directory\subdirectory\my_file.txt")
print(p.stem)
# my_file

If I had to do this with a regex, I'd do it like this:
s = re.sub(r'\.jpg$', '', s)

No need for regex. os.path.splitext is your friend:
os.path.splitext('1.1.1.jpg')
>>> ('1.1.1', '.jpg')

One can also use the string slicing.
>>> "1.1.1.1.1.jpg"[:-len(".jpg")]
'1.1.1.1.1'

Related

How to replace multiple forward slashes in a directory by a single slash?

My path:
'/home//user////document/test.jpg'
I want this to be converted into:
'/home/user/document/test.jpg'
How to do this?
Use os.path.abspath or normpath to canonicalise the path:
>>> import os.path
>>> os.path.abspath('/home//user////document/test.jpg')
'/home/user/document/test.jpg'
Solution:
This code snippet should solve your issue:
import re
x = '/home//user////document/test.jpg'
re.sub('/+','/', x)
Output:
'/home/user/document/test.jpg'
this solution is very simple by using Regex.
You can use it 're' module of the Python standard library.
import re
old_path = '/home//user////document/test.jpg'
converted_path = re.sub('/+', '/', old_path)
I'm sorry not to speak English fluently ;)
Instantiating a pathlib.Path object from your string will remove redundant slashes automatically for you:
from pathlib import Path
path = Path('/home//user////document/test.jpg')
print(path)
# /home/user/document/test.jpg
I think the easiest way is to replace '//' with '/' twice:
a = '/home//user////document/test.jpg'
a.replace('//', '/').replace('//', '/')

Python string alphabet removal?

So in my program, I am reading in files and processing them.
My output should say just the file name and then display some data
When I am looping through files and printing output by their name and data,
it displays for example: myfile.txt. I don't want the .txt part. just myfile.
how can I remove the .txt from the end of this string?
The best way to do it is in the example
import os
filename = 'myfile.txt'
print(filename)
print(os.path.splitext(filename))
print(os.path.splitext(filename)[0])
More info about this very useful builtin module
https://docs.python.org/3.8/library/os.path.html
The answers given are totally right, but if you have other possible extensions, or don't want to import anything, try this:
name = file_name.rsplit(".", 1)[0]
You can use pathlib.Path which has a stem attribute that returns the filename without the suffix.
>>> from pathlib import Path
>>> Path('myfile.txt').stem
'myfile'
Well if you only have .txt files you can do this
file_name = "myfile.txt"
file_name.replace('.txt', '')
This uses the built in replace functionality. You can find more info on it here!

How to replace a sentence in line of string address?

I need help replacing a part of string on a list of file's address location.
The file address looks like this :
/SfSNet/Images_mask/10_face.png
and I need to change it into something like this
/SfSNet/Images_mask/10_mask.png
I know it is possible to count the index since the front string are the same but it will be annoying in case I want to run the code on other PC. I read something about regex but it isn't clear for me. So maybe if someone can help me with this or have any other solution will be appreciated, thank you
Assuming the structure of all file names is as the above, you could use re.sub as:
s = '/SfSNet/Images_mask/10_face.png'
s.replace('_face.png', '_mask.png')
# '/SfSNet/Images_mask/10_mask.png'
If a simple str.replace lacks generality1, consider doing operations like this is with os.path.
>>> import os.path
>>>
>>> s = '/SfSNet/Images_mask/10_face.png'
>>> folder, file = os.path.split(s) # ('/SfSNet/Images_mask', '10_face.png')
>>> name, ext = os.path.splitext(file) # ('10_face', '.png')
>>> new_name = '{}_{}{}'.format(name.rsplit('_', 1)[0], 'mask', ext)
>>> os.path.join(folder, new_name)
'/SfSNet/Images_mask/10_mask.png'
1 For example, if you want to preserve the extension name without hardcoding it or if the substring you want to replace might appear in the directory name itself.

Get specific parts of a file path in Python

I have a path string like
'/path/eds/vs/accescontrol.dat/d=12520/file1.dat'
Q1: How can I get only accescontrol.dat from the path.
Q2: How can I get only /path/eds/vs/accescontrol.dat from the path.
import re
url = '/path/eds/vs/accescontrol.dat/d=12520/file1.dat'
match = re.search('^(.+/([^/]+\.dat))[^$]', url)
print match.group(1)
# Outputs /path/eds/vs/accescontrol.dat
print match.group(2)
# Outputs accescontrol.dat
I edited this to work in python2 and to answer both questions (the earlier regex answer above only answers the first of the two)
You could use regular expressions
import re
ma = re.search('/([^/]+\.dat)/d=', path)
print ma.group(1)
A simple solution is to use .split():
Q1:
str = '/path/eds/vs/accescontrol.dat/d=12520/file1.dat'
[x for x in str.split('/') if x[-4:] == '.dat']
gives:
['accescontrol.dat','file1.dat']
A similar trick will answer Q2.
For more advanced file path manipulation I would recommend reading about os.path
https://docs.python.org/2/library/os.path.html#module-os.path
I would recommend separating each level of folder/file into strings in a list.
path = '/path/eds/vs/accescontrol.dat/d=12520/file1.dat'.split("/")
This makes path = ['path', 'eds', 'vs', 'accescontrol.dat', 'd=12520', 'file1.dat']
Then from there, you can access each of the different parts.
Why not this way
from pathlib import Path
h=r'C:\Users\dj\Pictures\Saved Pictures'
path = Path(h)
print(path.parts)
Path: .
('C:\\', 'Users', 'dj', 'Pictures', 'Saved Pictures')

python:extract certain part of string

I have a string from which I would like to extract certain part. The string looks like :
E:/test/my_code/content/dir/disp_temp_2.hgx
This is a path on a machine for a specific file with extension hgx
I would exactly like to capture "disp_temp_2". The problem is that I used strip function, does not work for me correctly as there are many '/'. Another problem is that, that the above location will change always on the computer.
Is there any method so that I can capture the exact string between the last '/' and '.'
My code looks like:
path = path.split('.')
.. now I cannot split based on the last '/'.
Any ideas how to do this?
Thanks
Use the os.path module:
import os.path
filename = "E:/test/my_code/content/dir/disp_temp_2.hgx"
name = os.path.basename(filename).split('.')[0]
Python comes with the os.path module, which gives you much better tools for handling paths and filenames:
>>> import os.path
>>> p = "E:/test/my_code/content/dir/disp_temp_2.hgx"
>>> head, tail = os.path.split(p)
>>> tail
'disp_temp_2.hgx'
>>> os.path.splitext(tail)
('disp_temp_2', '.hgx')
Standard libs are cool:
>>> from os import path
>>> f = "E:/test/my_code/content/dir/disp_temp_2.hgx"
>>> path.split(f)[1].rsplit('.', 1)[0]
'disp_temp_2'
Try this:
path=path.rsplit('/',1)[1].split('.')[0]
path = path.split('/')[-1].split('.')[0] works.
You can use the split on the other part :
path = path.split('/')[-1].split('.')[0]

Categories