How do I get the filename from this string?
"C:\Documents and Settings\Usuario\Escritorio\hello\test.txt"
output:
"test.txt"
I really tried to find this one before posting, but all the results were contaminated, they talk about getting filenames from current dir (I must work with strings only)
split() method in Python is used to Split the path name into a pair head and tail. Here, tail is the last path name component and head is everything leading up to that. In the above example 'file. txt' component of path name is tail and '/home/User/Desktop/' is head.
To retrieve a file in Python, you need to know the exact path to reach the file, in Windows, you can view a particular file's path by right-clicking the File-> Properties-> General-> Location. Similarly, to run a script, the working directory needs to be set to the directory containing the script.
Method 1
for %%F in ("C:\Documents and Settings\Usuario\Escritorio\hello\test.txt") do echo %%~nxF
Type HELP FOR
for more info.
Method 2
call :sub "C:\Documents and Settings\Usuario\Escritorio\hello\test.txt" exit /b :sub echo %~nx1 exit /b
Type HELP CALL
for more info.
if your path comes as a parameter, just use:
%~nx1
(1 is for the first param and we suppose it's the first one)
echo %~nx1
would return directly test.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With