473,757 Members | 10,754 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(m yPath)
(fileBaseName, fileExtension)= os.path.splitex t(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($myPa th,
('\.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 12378
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.4 5-beta';
and this:

$myPath = 'x:.\example\ba sename.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
2788
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
4070
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 class library. How can I do this so I wont have to hardcode this path in my getpath() method. Here is the path below. "C:\Inetpub\wwwroot\Web_mycontrol\my_Files_dir\" -- Dotty
5
4220
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 = document.frm.file.value if(Text.indexOf(":\\") == 1)
11
8296
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 the executed program. so if i write anywhere:
3
2118
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 file but i need a full path of the file.... give me some solution ...... thanks ,
5
4919
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 the file paths all seem to include the drive letter for the drive they are stored on. This is not a problem in our office as users all have the same drive mapped to the same drive letter, however the database is also to be used at another site where...
34
5366
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 "<pyshell#10>", line 1, in <module> os.startfile("C:\Documents and Settings\Alex\My Documents\My
4
2839
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 program works correctly when calling it like this : ./program file1 file2, and also with full paths, like this : ./program c:\dir\file1 c:\dir\file2 However, a most puzzling phenomenon occurs when I use the full path only for the first file, like...
2
1895
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, System.Drawing.Imaging.ImageFormat.Gif); Now my situation is, i need to save it into my database(sqlserver). Can anyone tell me how to get that displayed(currently displayed) image file
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8737
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5172
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.