473,406 Members | 2,956 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,406 software developers and data experts.

Working with Strings

Hello all, I am new to python and in need of some aide. I am going through some Homework and am completely stumped and unable to find answers in any tutorials that are in plain enough english that I can understand.

my code is so far as followed:
Expand|Select|Wrap|Line Numbers
  1. def GetFileExtension(fullFileName):
  2.     print fullFileName[-4:]
  3.  
  4. def GetBaseName(fullFileName):
  5.     print fullFileName[:-4]
  6.  
  7. GetFileExtension("C:/MyFolder/Docs/Notes.txt")
  8. GetBaseName("C:/MyFolder/Docs/Notes.txt")
  9.  
Thus far the code will run and shows what is explained in the question, however; if the file extension was for example a .jpeg it would not work properly.

question for this question is as followed:

1. The “\” in Windows file paths are represented by “\\” or “/” in Python. Create a FileInfo.py and create two functions: GetFileExtension(fullFileName) and GetBaseFileName(fullFileName). Given “C:/MyFolder/Docs/Notes.txt”, GetFileExtension will return “.txt” and GetBaseName will return “C:/MyFolder/Docs/Notes”. At the top of FileInfo.py, write the code for these two functions. At the bottom of FileInfo.py, write calls to both of these functions using “C:/MyFolder/Docs/Notes.txt” as the argument.
HINTS: You will be using slicing and the len() function to perform this task.


any aide would be greatly appreciated.
Oct 16 '08 #1
3 2030
boxfish
469 Expert 256MB
To give you a general idea of what you need to do, you'll be starting at the end of the filename string and going backward until you find the last dot in the string. Then you slice it off. You should be able to do this with a loop. I hope this helps a bit.
Oct 16 '08 #2
asedt
125 100+
HINTS: You will be using slicing and the len() function to perform this task.

Hmm, I wold do it with Regular Expressions, but if the task is to do it with slicing and len() hmm.. I don't know how to do it.

But now you are using slice only so I guess the solution is in knowing how to use len() in the solution.


edit:

I think you can slice the last part of the string, slice the first letter of that string and se if its a dot. And start with slicing 1 char and then add 1 char mor to slice from the original string until the first char is a dot. (Then I think you need to use len() to)
Oct 17 '08 #3
boxfish
469 Expert 256MB
You just start at the end of the string (find it with len). Is it a dot? No. Then step back one character. Dot? No. Go back. Is it a dot? Yes. Now you have the index of the dot character. Slice off everything after that.
Hope this makes sense.
Oct 17 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Adam T. Gautier | last post by:
I have been unable to solve a problem. I am working with MD5 signatures trying to put these in a database. The MD5 signatures are not generated using the python md5 module but an external...
2
by: Shaun | last post by:
Hi Odd one here, I have a local version of SQL running on my laptop for demos, been working fine for years yet yesterday I suddenly get an error "Invalid Conection Parameter" running a sproc...
0
by: Robert Misiak | last post by:
Hi all- I'm trying to add Windows 98/ME support to an app that previously only supported Windows 2000+. My test box is a running 98 SE. I have a number of localized forms (translated for...
2
by: Noozer | last post by:
Hi! Trying to get my head around working with network connectivity in VB.Net. I've gotten a fairly decent control based on the sample in the 101VBSamples available from Microsoft.What I'm having...
2
by: John | last post by:
I am trying to call a third party company's dll that I have no control over. They tell me it was written in C. The declaration in VB6 is as follows: Private Declare Sub csub Lib "rlpolk.dll"...
3
by: R Reyes | last post by:
Error: Unable to cast object of type 'ASP.CompanyDirectory_aspx' to type 'CompanyDirectory'. Code: clsCompanyDirectory = (CompanyDirectory)Context.Handler;...
6
by: Ptaku25 | last post by:
Hi all // my code var string1= "Pablo has 3 cats and 1 dog"; var string2= "Pablo has 3 cats"; var str1 = ""; var str2 = ""; str1 = string1.match(string1, "g");
1
by: suresh | last post by:
Hello all, I have a problem while working with fixed length stings in Vb.net The problem is, when I call a Function FormInfo to return a FormName, which is a string value, it is returning some...
2
by: John Nagle | last post by:
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File...
3
by: Jon Slaughter | last post by:
mcrypt_enc_get_iv_size is not randomizing the vector. I have tried to randomize the seed in various ways but it always returns the same output. My current code as follows: for($i = 1; $i < 5;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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 project—planning, coding, testing,...

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.