How can I replace (or strip) an extension from a filename in Python?

Is there a built-in function in Python that would replace (or remove, whatever) the extension of a filename (if it has one)? Example: print replace_extension(‘/home/user/somefile.txt’, ‘.jpg’) In my example: /home/user/somefile.txt would become /home/user/somefile.jpg I don’t know if it matters, but I need this for a SCons module I’m writing. (So perhaps there is some SCons … Read more

Get file name from URI string in C#

I have this method for grabbing the file name from a string URI. What can I do to make it more robust? private string GetFileName(string hrefLink) { string[] parts = hrefLink.Split(“https://stackoverflow.com/”); string fileName = “”; if (parts.Length > 0) fileName = parts[parts.Length – 1]; else fileName = hrefLink; return fileName; } 9 Answers 9

Build the full path filename in Python

I need to pass a file path name to a module. How do I build the file path from a directory name, base filename, and a file format string? The directory may or may not exist at the time of call. For example: dir_name=”/home/me/dev/my_reports” base_filename=”daily_report” format=”pdf” I need to create a string ‘/home/me/dev/my_reports/daily_report.pdf’ Concatenating the … Read more

What is the naming standard for path components?

I keep getting myself in knots when I am manipulating paths and file names because I don’t follow a naming standard for path components. Consider the following toy problem (Windows example, but hopefully the answer should be platform independent). You have been given the path of a folder: C:\users\OddThinking\Documents\My Source\ You want to walk the … Read more