Trouble getting file name with extension using pathlib - python

I have this code which gives me the filename no problem. It gives it to me without the extension. Any way to get with extension?
from pathlib import Path
file = 'somepath'
path_object = Path(file)
filename = path_object.stem

You can use .name attribute of the Path object.
>>> from pathlib import Path
>>>
>>> p = Path("SomePath/filename.txt")
>>> p.name
'filename.txt'

Related

How can i change file name while copying them using shutil?

I am using shutil to copy my files to another destination directory , what i want to achieve is
src/red.png
src/blue.png
src/green.png
for example if i select blue and green then while saving i want them to be saved as
dst/blue_1001.png
dst/green_1002.png
Thanks
Kartikey
All you need to do is to construct a destination path in a format that you want and shutil will do rest of the job.
I am using pathlib module here because it provides convenient APIs to work with paths.
>>> import shutil
>>> from pathlib import Path
>>>
>>> files_to_copy = ["src/red.png", "src/blue.png"]
>>>
>>> destination = Path("dst")
>>>
>>> for index, file in enumerate(map(Path, files_to_copy), start=1001):
... destination_filename = f"{file.stem}_{index}{file.suffix}"
... print(f"{destination_filename=}")
... _ = shutil.copy(file, destination / destination_filename)
...
destination_filename='red_1001.png'
destination_filename='blue_1002.png'
Shutils supports setting the name of the target file. It mainly depends on how you splice the name of the target file
import shutil
from pathlib import Path
parent_path = Path(__file__).parent
file_path = parent_path.joinpath("test2.py")
shutil.copy(file_path, parent_path.joinpath("test3.py"))

Raster to ASCII Conversion [duplicate]

Suppose from index.py with CGI, I have post file foo.fasta to display file. I want to change foo.fasta's file extension to be foo.aln in display file. How can I do it?
An elegant way using pathlib.Path:
from pathlib import Path
p = Path('mysequence.fasta')
p.rename(p.with_suffix('.aln'))
os.path.splitext(), os.rename()
for example:
# renamee is the file getting renamed, pre is the part of file name before extension and ext is current extension
pre, ext = os.path.splitext(renamee)
os.rename(renamee, pre + new_extension)
import os
thisFile = "mysequence.fasta"
base = os.path.splitext(thisFile)[0]
os.rename(thisFile, base + ".aln")
Where thisFile = the absolute path of the file you are changing
Starting from Python 3.4 there's pathlib built-in library. So the code could be something like:
from pathlib import Path
filename = "mysequence.fasta"
new_filename = Path(filename).stem + ".aln"
https://docs.python.org/3.4/library/pathlib.html#pathlib.PurePath.stem
I love pathlib :)
Use this:
os.path.splitext("name.fasta")[0]+".aln"
And here is how the above works:
The splitext method separates the name from the extension creating a tuple:
os.path.splitext("name.fasta")
the created tuple now contains the strings "name" and "fasta".
Then you need to access only the string "name" which is the first element of the tuple:
os.path.splitext("name.fasta")[0]
And then you want to add a new extension to that name:
os.path.splitext("name.fasta")[0]+".aln"
As AnaPana mentioned pathlib is more new and easier in python 3.4 and there is new with_suffix method that can handle this problem easily:
from pathlib import Path
new_filename = Path(mysequence.fasta).with_suffix('.aln')
Using pathlib and preserving full path:
from pathlib import Path
p = Path('/User/my/path')
new_p = Path(p.parent.as_posix() + '/' + p.stem + '.aln')
Sadly, I experienced a case of multiple dots on file name that splittext does not worked well... my work around:
file = r'C:\Docs\file.2020.1.1.xls'
ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
filefinal = file.replace(ext,'')
filefinal = file + '.zip'
os.rename(file ,filefinal)
>> file = r'C:\Docs\file.2020.1.1.xls'
>> ext = '.'+ os.path.realpath(file).split('.')[-1:][0]
>> filefinal = file.replace(ext,'.zip')
>> os.rename(file ,filefinal)
Bad logic for repeating extension, sample: 'C:\Docs\.xls_aaa.xls.xls'

How to remove a file from Path object in Pathlib module?

I have a Path Object representing C:\Users\users\Downloads\img.jpg. How do I get it so the Path only represents C:\Users\user\Downloads? I don't want to delete the file, but rather go back in the Path object itself.
from pathlib import Path
path = Path('C:/Users/user/Downloads/img.jpg')
# Want to get path only to C:\Users\user\Downloads
I would utilize the PurePath class within pathlib as follows:
from pathlib import PurePath
path = PurePath('C:/Users/user/Downloads/img.jpg')
parent = path.parents[0]
This yields: PureWindowsPath('C:/Users/users/Downloads')

Extract a part of the filepath (a directory) in Python

I need to extract the name of the parent directory of a certain path. This is what it looks like:
C:\stuff\directory_i_need\subdir\file.jpg
I would like to extract directory_i_need.
import os
## first file in current dir (with full path)
file = os.path.join(os.getcwd(), os.listdir(os.getcwd())[0])
file
os.path.dirname(file) ## directory of file
os.path.dirname(os.path.dirname(file)) ## directory of directory of file
...
And you can continue doing this as many times as necessary...
Edit: from os.path, you can use either os.path.split or os.path.basename:
dir = os.path.dirname(os.path.dirname(file)) ## dir of dir of file
## once you're at the directory level you want, with the desired directory as the final path node:
dirname1 = os.path.basename(dir)
dirname2 = os.path.split(dir)[1] ## if you look at the documentation, this is exactly what os.path.basename does.
For Python 3.4+, try the pathlib module:
>>> from pathlib import Path
>>> p = Path('C:\\Program Files\\Internet Explorer\\iexplore.exe')
>>> str(p.parent)
'C:\\Program Files\\Internet Explorer'
>>> p.name
'iexplore.exe'
>>> p.suffix
'.exe'
>>> p.parts
('C:\\', 'Program Files', 'Internet Explorer', 'iexplore.exe')
>>> p.relative_to('C:\\Program Files')
WindowsPath('Internet Explorer/iexplore.exe')
>>> p.exists()
True
All you need is parent part if you use pathlib.
from pathlib import Path
p = Path(r'C:\Program Files\Internet Explorer\iexplore.exe')
print(p.parent)
Will output:
C:\Program Files\Internet Explorer
Case you need all parts (already covered in other answers) use parts:
p = Path(r'C:\Program Files\Internet Explorer\iexplore.exe')
print(p.parts)
Then you will get a list:
('C:\\', 'Program Files', 'Internet Explorer', 'iexplore.exe')
Saves tone of time.
First, see if you have splitunc() as an available function within os.path. The first item returned should be what you want... but I am on Linux and I do not have this function when I import os and try to use it.
Otherwise, one semi-ugly way that gets the job done is to use:
>>> pathname = "\\C:\\mystuff\\project\\file.py"
>>> pathname
'\\C:\\mystuff\\project\\file.py'
>>> print pathname
\C:\mystuff\project\file.py
>>> "\\".join(pathname.split('\\')[:-2])
'\\C:\\mystuff'
>>> "\\".join(pathname.split('\\')[:-1])
'\\C:\\mystuff\\project'
which shows retrieving the directory just above the file, and the directory just above that.
import os
directory = os.path.abspath('\\') # root directory
print(directory) # e.g. 'C:\'
directory = os.path.abspath('.') # current directory
print(directory) # e.g. 'C:\Users\User\Desktop'
parent_directory, directory_name = os.path.split(directory)
print(directory_name) # e.g. 'Desktop'
parent_parent_directory, parent_directory_name = os.path.split(parent_directory)
print(parent_directory_name) # e.g. 'User'
This should also do the trick.
This is what I did to extract the piece of the directory:
for path in file_list:
directories = path.rsplit('\\')
directories.reverse()
line_replace_add_directory = line_replace+directories[2]
Thank you for your help.
You have to put the entire path as a parameter to os.path.split. See The docs. It doesn't work like string split.

Get path from open file in Python

If I have an opened file, is there an os call to get the complete path as a string?
f = open('/Users/Desktop/febROSTER2012.xls')
From f, how would I get "/Users/Desktop/febROSTER2012.xls" ?
The key here is the name attribute of the f object representing the opened file. You get it like that:
>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> f.name
'/Users/Desktop/febROSTER2012.xls'
Does it help?
I had the exact same issue. If you are using a relative path os.path.dirname(path) will only return the relative path. os.path.realpath does the trick:
>>> import os
>>> f = open('file.txt')
>>> os.path.realpath(f.name)
And if you just want to get the directory name and no need for the filename coming with it, then you can do that in the following conventional way using os Python module.
>>> import os
>>> f = open('/Users/Desktop/febROSTER2012.xls')
>>> os.path.dirname(f.name)
>>> '/Users/Desktop/'
This way you can get hold of the directory structure.
You can get it like this also.
filepath = os.path.abspath(f.name)

Categories