BeautifulSoup chokes on paths with back slashes

I wrote a script to automate the process of creating an image gallery. I used os.path.join() for creating paths to new image directories.

I only relized after creating all the galleries that using os.path.join() was not such a good idea as it creates paths with \ (on windows) which causes problems with firefox (it doesn't seem to understand the path format and cant find the images).

Id rather not have to create all the galleries again since the gallery headers have to be entered manually. I thought BeautifulSoups prettify() would fix the paths but it chokes on the backslashes. e.g.

input:

<td><a rel="group" href="images42.jpg"><img class="gimage" src="images\thumbnails\thumb_042.jpg" alt=""></a></td>

output:

<td> <a rel="example_group" href="images"> <img class="gimage" src="images humbnails humb_042.jpg" alt="" /> </a> </td>

How can I fix the paths?

-------------Problems Reply------------

In this case, per the comments, it appears that the problem can be solved with a global substitution of / for \:

import fileinput
import sys
for line in fileinput.input(['test.html'], inplace=True, backup='.bak'):
sys.stdout.write(line.replace('\\','/'))

Category:python Views:7 Time:2010-09-07

Related post

  • Is there a way to force PHP on Windows to provide paths with forward slashes? 2011-06-28

    I'm the only Windows developer in my office, and we have a lot of code that looks like this: $fileName = preg_replace("/^(extend|base)\//", "", $fileName); $fileName generally comes from FILE or something similar. In order to get this code to work on

  • Full path with double slash (C#) 2012-02-15

    Is it possible to get a full path with double slash by using Path.GetFullPath? Something like this: C:\\Users\\Mammamia\\Videos\\Documents\\CFD\\geo_msh\\cubeOp.txt instead of this: C:\Users\Mammamia\Videos\Documents\CFD\geo_msh\cubeOp.txt Or is ther

  • parse this directory path without losing slash 2010-05-18

    I have a wxPython application. I am taking in a directory path from a textbox using GetValue(). I notice that while trying to write this string to a variable: "C:\Documents and Settings\tchan\Desktop\InputFile.xls", python sees the string as 'C:\\Doc

  • PHP: a good way to universalize paths across OSs (slash directions) 2011-04-12

    My simple concern is being able to handle paths across OSs, mainly in the regard of back and forward slashes for directory separators. I'm aware of DIRECTORY_SEPARATOR, However: 1. It's long to write 2. Paths may come from different sources, not nece

  • .NET new Uri(base,relative) bug when relative path has 3+ slashes in the beginning 2011-04-03

    While creating a new Uri if the second parameter (relativeUri) is starting with 3 slashes then Dim googleUri AS New Uri("http://www.google.com/") Dim threeSlashRel as New Uri(googleUri,"///page.htm") This code will throw the following exception: The

  • Should a directory path variable end with a trailing slash? 2009-06-11

    When defining a path to a directory as a variable or constant, should it end with a trailing slash? What is the convention? pwd in unix shows your current directory without a trailing slash, while the tab complete of cd /var/www/apps/ includes the tr

  • So what IS the right direction of the path's slash (/ or \) under Windows? 2009-10-19

    It seems Windows insists on writing a backslash \ in file paths, whereas .NET's URI class writes them with a slash /. Is there any right way, that is accepted even in the most primitive systems? And why is .NET's URI showing the other slash compared

  • Can't open file on volume with slashes in the file path 2014-04-27

    Hello I am facing the same problem - are there any ideas as to how I might be able to recover my presentation? I use a Macbook pro. My error message is: "Powerpoint cannot open the file/ Volumes/Seagate Backup Plus Drive/UNICEF5/Presentations/Retreat

  • Handling file paths cross platform 2008-09-23

    Do any C++ GNU standalone classes exist which handle paths cross platform? My applications build on Windows and LInux. Our configuration files refer to another file in a seperate directory. I'd like to be able to read the path for the other configura

  • Include header path change from Windows to Linux 2009-02-21

    I'm porting an application written in C++ from Windows to Linux. I have a problem with the header files path. Windows uses \ and Linux uses /. I am finding it cumbersome to change this in each and every source and header file. Is there some work arou

  • Explain relative paths - VWD vs IIS 2009-08-17

    Can someone breifly explain how c# treats paths - or perhaps the development environment and VWD (that is not acting as I think a server should)? I have a page: /content/consumer/MyPage.aspx. My images are all in /images. When I put src="/images/myim

  • Creating Uri from base without trailing slash and relative parts 2009-10-13

    I am having a problem with Uri constructor. Results differ on whether base path ends with slash or not. var baseWithSlash = new Uri("c:\\Temp\\"); var baseNoSlash = new Uri("c:\\Temp"); var relative = "MyApp"; var pathWithSlash = new Uri(baseWithSlas

  • How can I normalize/collapse paths or URLs in Python in OS independent way? 2010-01-25

    I tried to use os.normpath in order to convert http://example.com/a/b/c/../ to http://example.com/a/b/ but it doesn't work on Windows because it does convert the slash to backslash. --------------Solutions------------- Here is how to do it >>

  • What Path is acceptable? 2010-02-19

    I am using below statement to return the directory name of the running script: print dirname(__FILE__); it outputs something like this with back-slashes: www\EZPHP\core\ezphp.php Question: Is a path with back-slashes acceptable across all major opera

  • Customize save path for ImageField 2010-09-07

    I want to customize the folders that are used to save my images for a record...currently, I have: original_image = models.ImageField(upload_to='photos') But what I want to have is the images being saved to photos/<vehicle's_stock_number>/...how

  • Google Chrome extension relative path 2010-10-18

    I'm developing a google chrome extension and I'm running into a relative path problem. If I give a relative path to an image and open the plugin in a certain page it will look for that image in the website's path rather than the extension's. Any idea

  • Replace Backslashes with Forward Slashes in Python 2010-11-07

    I'm writing a cross platform file explorer in python. I am trying to convert any backslashes in a path into forward slashes in order to deal with all paths in one format. I've tried not only using string.replace(str, '\\', '/'), but also creating a m

  • How can I display the ending path of a url in rails? 2010-12-09

    I can display a path like this: <%= request.request_uri %> or <%= request.path %> And I will get something like this: /controller/action/foo1/foo2 How can I just get the ending path without a slash? Like in this example, it would be: "foo

  • How can I extract the file name and extension from a path in C++ 2010-12-13

    I have a list of files stored in a .log in this syntax: c:\foto\foto2003\shadow.gif D:\etc\mom.jpg I want to extract the name and the extension from this files. Can you give a example of a simple way to do this? --------------Solutions------------- T

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.127 (s). 11 q(s)