473,411 Members | 1,918 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Perl-Python-a-Day: split a file full path

Split File Fullpath Into Parts

Xah Lee, 20051016

Often, we are given a file fullpath and we need to split it into the
directory name and file name. The file name is often split into a core
part and a extension part. For example:

'/Users/t/web/perl-python/I_Love_You.html'
becomes

'/Users/t/web/perl-python/' (directory name)
'I_Love_You' (file's base name)
'.html' (file's “extension”)
Depending on the language, some language will remove the trailing slash
after the dir name, and some will omit the dot before the suffix.

In Python, to split a full path into parts is done with the os.path
module. Example:

# python
import os.path

myPath = '/Users/t/web/perl-python/I_Love_You.html'
(dirName, fileName) = os.path.split(myPath)
(fileBaseName, fileExtension)=os.path.splitext(fileName)

print dirName # /Users/t/web/perl-python
print fileName # I_Love_You.html
print fileBaseName # I_Love_You
print fileExtension # .html
The official doc of the os.path module is at:
http://www.python.org/doc/2.4.1/lib/module-os.path.html

In Perl, spliting a full path into parts is done like this:

# perl
use File::Basename;

$myPath = '/Users/t/web/perl-python/I_Love_You.html';

($fileBaseName, $dirName, $fileExtension) = fileparse($myPath,
('\.html') );

print $fileBaseName, "\n"; # I_Love_You
print $dirName, "\n"; # /Users/t/web/perl-python/
print $fileExtension, "\n"; # .html
Note: the second argument to fileparse() is a list of regex. In
particular, you need to escape the dot.

For the official doc, type in command line: “perldoc File::Path”.
------
This post is archived at
http://xahlee.org/perl-python/split_fullpath.html

Schemers, a scsh version will be appreciated.

Xah
xa*@xahlee.org
http://xahlee.org/

Oct 17 '05 #1
3 12357
Xah Lee:
In Perl, spliting a full path into parts is done like this:


And then follows Perl-code that only works with an optional .html
"extension",
which is similar to the code in the File::Basename description.
http://www.perl.com/doc/manual/html/.../Basename.html
It is best practice to derive and store the normalized (or absolute)
path, because relative paths can get loose so will get loose.
Consider this:

$myPath = './example/basename.ext';
and this:

$myPath = './example/filename.1.23.45-beta';
and this:

$myPath = 'x:.\example\basename.ext';
(some platforms have a wd per device)
--
Affijn, Ruud

"Gewoon is een tijger."

Oct 17 '05 #2
Hello, I'm a cs student from Milano (Italy).
I don't use scsh fequently but this should work:

(open srfi-11 ;let-values
srfi-28) ;format

(define my-path "/Users/t/web/perl-python/I_Love_You.html")

(let-values (((dir-name
file-base-name
file-extension) (parse-file-name my-path)))

(format "~a~%" dir-name) ;/Users/t/web/perl-python/
(format "~a~%" file-base-name) ;I_Love_You
(format "~a~%" file-extension)) ;.html

You can find more useful function here (scsh reference manual):
http://www.scsh.net/docu/html/man-Z-...l#node_sec_5.1

matteo

Oct 17 '05 #3
Xah Lee wrote:
In Perl, spliting a full path into parts is done like this:

Dr.Ruud wrote: And then follows Perl-code that only works with an optional .html
"extension",


Thanks for the note. I've corrected it here:
http://xahlee.org/perl-python/split_fullpath.html

namely:
Note: the second argument to fileparse() is a list of regex. In
particular, you need to escape the dot. Normally, one gives it a value
such as ('\.html', '\.HTML', '\.jpg', '\.JPG'). Yes, it is case
sensitive. If you want to match any extension (that is, the string
after the last dot), use ('\.[^.]+$').

Xah
xa*@xahlee.org
http://xahlee.org/

Oct 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Benjamin Han | last post by:
I know I can do this by get sys.argv, tell if it's a full path, and if not, somehow join the relative path with getcwd(). Just wondering if there's a simpler way to do this. Thanks!
4
by: Dots | last post by:
I have a class library with a method called getpath(). I want to be able to get the full path of a folder and write some files to the (my_files_dir) folder. A console application will use this...
5
by: Smarty | last post by:
Dear Friends, I did a file type validation in Javascript. Just i want to check whether a selected file is BMP or JPG. I wrote the following function for it. function checkType(){ Text =...
11
by: rh00667 | last post by:
hi all, i'm confused now. how i can get the full path of an application? if myapp is in a directory which belongs to PATH, argv gives me the first token of cmd line, and not the real path of...
3
kaleeswaran
by: kaleeswaran | last post by:
Hi ! In the struts i am using <html:file> getting the url of the file and getting that file into the action class using StrutsActionForm oaky. now my problem is i can get the base name of the...
5
ollyb303
by: ollyb303 | last post by:
Hello, I have a small problem that I hope someone can help with. My Access 2000 database uses several tables and is split into BE and FE. In the FE the tables are all linked to the BE but...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
4
by: Michel Rouzic | last post by:
I made a program that accepts as parameters an input file name that we'll call file1, and an output file name that we'll call file2, and opens on its own a configuration file called file3. My...
2
by: =?Utf-8?B?U2hhbQ==?= | last post by:
Hai, I am developing application which handles to create thumbnail form a video file. I am successfully created image and i displayed it into browser by, bit.Save(Response.OutputStream,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.