0

I'm getting the name of a file in Django after an Image save :

path->    'companies/92_dsa/log/Hydrangeas.jpg' as it is in database

I do a clone of the file, an resize (is an image) and want to save the new file with a different name.

I get the directory of the original file:

folder = os.path.dirname(path)

the filename and extension:

  filename, extension = os.path.splitext(os.path.basename(media_path))

then create a

new_filename =  filename + '_sz' + extension

and the I want to recreate the path:

new_path = os.path.join(folder, new_filename)

and the problem(slash-backslash before the filename):

'companies/94_sda/logos\Hydrangeas_sz.jpg'

I'm working in Windows, bur the final deploy probably will be on Linux, so I want a fix indifferent of the OS.

user3541631
  • 3,686
  • 8
  • 48
  • 115
  • 4
    It's not clear what the issue is. `os.path.join()` works correctly regardless of OS. –  Jan 09 '18 at 18:53
  • @blurp Only if the filenames belong to the host OS – kdopen Jan 09 '18 at 18:54
  • @kdopen Maybe, but that doesn't appear to be relevant here. –  Jan 09 '18 at 18:57
  • 1
    @Blurp It's actually the root cause of the OP's problem. Django is providing a Linux style filename. The script is running on Windows, so uses "\" as the path separator. – kdopen Jan 09 '18 at 18:59
  • @kdopen OP doesn't actually state what the problem is. It sounds like they are doing development on Windows and deploying to Linux, in which case there shouldn't be an issue... _unless_ they're doing something odd like creating records locally and copying them to the production database. –  Jan 09 '18 at 19:01
  • "I'm working in Windows, bur the final deploy **probably** will be on Linux". Implying that right now they are running on their development platform. – kdopen Jan 09 '18 at 19:02

2 Answers2

2

so I want a fix indifferent of the OS.

Unfortunately, you can't really have your cake and eat it.

You say that

I'm working in Windows, bur the final deploy probably will be on Linux

This implies you are running the program on Windows, but dealing with *nix file names (be it Linux, Unix, or mac OS).

To do this completely os-independent ... you would need to split the original path on "/" to get all the sub components and then re-join them with os.path.join.

But then you need to deal with the fact that directory structures for absolute paths are very different between the two OS's - not to mention the leading drive specifier on Windows. This is less of an issue if you are only dealing with relative paths.

In short, the root of your problem is that the database contains Linux-style paths and you are processing them on Windows. You would have a similar problem if it was the other way around.

You need to choose your deployment platform and code for it.

Alternatively, write your code to simply remove the extension from the full path and replace it with "_sz."+extension

kdopen
  • 8,032
  • 7
  • 44
  • 52
0

Since you don't actually care about the path in relation to the host OS (because you've chosen to store paths POSIX style in your DB), you can just use string joining: new_path = '/'.join([folder, new_filename]), or you could import the posixpath module directly import posixpath; new_path = posixpath.join(folder, new_filename).

You could also investigate PathLib, though that may be overkill for you.

mth
  • 486
  • 4
  • 8
  • @actually I didn't chose, is how the Django Framework is setting, what I want is to do the development in windows, and if the server will be in linux, the code to work. – user3541631 Jan 09 '18 at 19:41
  • My main point was that your file paths are stored POSIX style in the database, i.e. `/path/to/file.ext` as opposed to NT style i.e. `C:\path\to\file.ext`, which means you don't care about joining the path with host operating system directory separators, and as such you should write your code to treat the paths as POSIX regardless of OS by either using string joining (which won't ever change because it's explicit what character you're joining the strings with) or using the `posixpath` which is what most POSIX compatible systems use. – mth Jan 09 '18 at 20:12