Re: absolute path to a file
Thanks. Hope you found a solution to the problem. On Tue, Aug 20, 2019, 2:51 AM Cameron Simpson wrote: > Please remember to CC the list. > > On 19Aug2019 22:06, Paul St George wrote: > >On 19/08/2019 14:16, Cameron Simpson wrote: > [...] > >>There's a remark on that web page I mentioned that suggests that the > >>leading '//' indicates the filename is relative to the Blender model, > >>so the context directory for the '//' is likely > >>/Users/Lion/Desktop/test8. > > > >Yes. That makes sense. The reason I was testing with two images, one > >at /Users/Lion/Desktop/test8/image01.tif and the other at > >/Users/Lion/Desktop/images/image02.tif is that I cannot rely on images > >being in the same folder as the Blender file. > > > >So, let's assume the context directory is /Users/Lion/Desktop/test8 > >and see how we get on below. > [...] > >>realpath needs a UNIX path. Your //image01.tif isn't a UNIX path, it > >>is a special Blender path. First you need to convert it. By replacing > >>'//' with the blend file's directory. Then you can call realpath. If > >>you still need to. > > > >Understood. Now. Thanks! > >> > >>[...snip...] > > > >Did you just [...snip...] yourself? > > Yes. It keeps the surrounding context manageable. In this way you know > to which text I am referring, without having to wade through paragraphs > to guess what may be relevant. > > >>from os.path import dirname > >> > >># Get this from somewhere just hardwiring it for the example. > >># Maybe from your 'n' object below? > >>blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' > >Is this setting a relative path? > >> > >>blender_image_file = n.image.filename > >> > >>unix_image_file = unblenderise(blender_image_file, > dirname(blend_file)) > >> > >>Now you have a UNIX path. If blend_file is an absolute path, > >>unix_image_path will also be an absolute path. But if blend_file is > >>a relative path (eg you opened up "tifftest8.blend") unix_image_path > >>will be a relative path. > > > >Does unix_image_path = unix_image_file? > > Yeah, sorry, my mistake. > > >Two possibilities here. > >blend_file (and so unix_image_file) is an absolute path OR blend_file > >(and so unix_image_file) is a relative path. > > > >I just want to check my understanding. If I supply the path to > >blend_file then it is absolute, and if I ask Python to generate the > >path to blend_file from within Blender it is relative. Have I got it? > > Not quite. What seems to be the situation is: > > You've got some object from Blender called "n.image", which has a > ".file" attribute which is a Blender reference to the image file of the > form "//image01.tif". > > I presume that Blender has enough state inside "n" or "n.image" to > locate this in the real filesystem; maybe it has some link to the > Blender model of your blend file, and thus knows the path to the blend > file and since //image01.tif is a reference relative to the blend file, > it can construct the UNIX path to the file. > > You want to know the UNIX pathname to the image file (maybe you want to > pass it to some unrelated application to view the file or something). > > So you need to do what Blender would do if it needs a UNIX path (eg to > open the file). > > The formula for that is dirname(path_to_blendfile) with n.image.file[2:] > appended to it. So that's what we do with unblenderise(): if the > filename is a "Blender relative name", rip off the "//" and prepend the > blend file directory path. That gets you a UNIX path which you can hand > to any function expecting a normal operating system pathname. > > Whether that is an absolute path or a relative path is entirely "does > the resulting path start with a '/'"? > > An absolute path starts with a slash and is relative to the root of the > filesystem. You can use such a path regardless of what your current > working directory is, because it doesn't use the working directory. > > A relative path doesn't start with a slash and is relative to the > current working directory. It only works if you're in the right working > directory. > > _Because_ a relative path depends on the _your_ working directory, > usually we pass around absolute paths if we need to tell something else > about a file, because that will work regardless if what _their_ working > directory may be. > > So, you may well want to turn a relative path into an absolute path... > > >If I decided not to supply the path and so ended up with a relative > >UNIX path, I could now use realpath or abspath to find the absolute > >path. Have I still got it? > > This is correct. > > Abspath may even call realpath to do its work, unsure. > > >It works very well. So thank you! I tested it with a Blend file that > >had two images, one in the same folder as the Blend file and the other > >was in a folder on the Desktop called 'images'. > > > >The initial results were: > >Plane uses image01.tif saved at //image01.tif which is at > >/Users/Lion/Desktop/test8/image01.tif > >Plane
Re: absolute path to a file
On 21/08/2019 04:09, Grant Edwards wrote: On 2019-08-21, Richard Damon wrote: I think gmane feed the newsgroup comp.lang.python which feeds python-list@python.org. No, gmane is a gateway to python-list@python.org. -- Grant I use https://mail.python.org/pipermail/python-list/ to confirm that any post has gone where it should. And it has. -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 2019-08-21, Richard Damon wrote: > I think gmane feed the newsgroup comp.lang.python which feeds > python-list@python.org. No, gmane is a gateway to python-list@python.org. -- Grant -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 8/20/19 5:56 PM, Cameron Simpson wrote: > > Hmm. I've been getting some of your posts directly to me as email with > no obvious python-list@python.org to/cc header. Maybe some interaction > with gmane? If you've been posting to the gmane newsgroup and CCing me > privately that is likely fine, and I've misread the event (provided > gmane backfeeds to the mailing list). I think gmane feed the newsgroup comp.lang.python which feeds python-list@python.org. Python-list probably then sees that you already were getting a direct copy so omits sending you the duplicate. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 20Aug2019 21:06, Paul St George wrote: On 20/08/2019 11:43, Cameron Simpson wrote: Please remember to CC the list. Instead of 'Post a followup to this newsgroup' or 'To: python-list@python.org'? Hmm. I've been getting some of your posts directly to me as email with no obvious python-list@python.org to/cc header. Maybe some interaction with gmane? If you've been posting to the gmane newsgroup and CCing me privately that is likely fine, and I've misread the event (provided gmane backfeeds to the mailing list). [...] We used blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' So, if I read you correctly, this is an absolute path. When we used unblenderise on it, out popped a path that was partly but not wholly relative. The latter half of the path is relative to the absolute first half (/Users/Lion/Desktop/test8/). /Users/Lion/Desktop/test8/../images/image02.tif It starts with a slash but cannot be pasted into "Go to...". But maybe that is an absolute path in UNIX? Yes. It starts with a slash. If you hand this to abspath it would fold out the "test8/.." part and give you a more direct absolute path. And realpath would traverse the path looking for symlinks etc and fold out the same pair as a side effect of traversing the path. But realpath is symlink aware. If test8 were a symlink to some remote part of the filesystem so that the TIF image were also elswhere, you'd get a direct path to that remote location. For example, on our home server my ~/media directory is actually a symlink to an area on our 8TB RAID-1 volume: [~]borg*> pwd /home/cameron [~]borg*> ls -ld media lrwxrwxrwx 1 cameron cameron 9 Nov 2 2017 media -> 8TB/media In fact "8TB" is also a symlink. So let's look: [~]borg*> python3 Python 3.7.1 (default, Nov 14 2018, 10:38:43) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from os.path import abspath, realpath >>> abspath('media') '/home/cameron/media' >>> realpath('media') '/app8tb/cameron/media' Both are valid absolute paths to the media directory. Realpath has follwed the symlinks and given a direct route from '/' which does not go through any symlinks. Abspath has not bothered with such effort. Cheers, Cameron Simpson (formerly c...@zip.com.au) -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 20/08/2019 11:43, Cameron Simpson wrote: Please remember to CC the list. Instead of 'Post a followup to this newsgroup' or 'To: python-list@python.org'? On 19Aug2019 22:06, Paul St George wrote: On 19/08/2019 14:16, Cameron Simpson wrote: [...] There's a remark on that web page I mentioned that suggests that the leading '//' indicates the filename is relative to the Blender model, so the context directory for the '//' is likely /Users/Lion/Desktop/test8. Yes. That makes sense. The reason I was testing with two images, one at /Users/Lion/Desktop/test8/image01.tif and the other at /Users/Lion/Desktop/images/image02.tif is that I cannot rely on images being in the same folder as the Blender file. So, let's assume the context directory is /Users/Lion/Desktop/test8 and see how we get on below. [...] realpath needs a UNIX path. Your //image01.tif isn't a UNIX path, it is a special Blender path. First you need to convert it. By replacing '//' with the blend file's directory. Then you can call realpath. If you still need to. Understood. Now. Thanks! [...snip...] Did you just [...snip...] yourself? Yes. It keeps the surrounding context manageable. In this way you know to which text I am referring, without having to wade through paragraphs to guess what may be relevant. from os.path import dirname # Get this from somewhere just hardwiring it for the example. # Maybe from your 'n' object below? blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' Is this setting a relative path? blender_image_file = n.image.filename unix_image_file = unblenderise(blender_image_file, dirname(blend_file)) Now you have a UNIX path. If blend_file is an absolute path, unix_image_path will also be an absolute path. But if blend_file is a relative path (eg you opened up "tifftest8.blend") unix_image_path will be a relative path. Does unix_image_path = unix_image_file? Yeah, sorry, my mistake. Two possibilities here. blend_file (and so unix_image_file) is an absolute path OR blend_file (and so unix_image_file) is a relative path. I just want to check my understanding. If I supply the path to blend_file then it is absolute, and if I ask Python to generate the path to blend_file from within Blender it is relative. Have I got it? Not quite. What seems to be the situation is: You've got some object from Blender called "n.image", which has a ".file" attribute which is a Blender reference to the image file of the form "//image01.tif". I presume that Blender has enough state inside "n" or "n.image" to locate this in the real filesystem; maybe it has some link to the Blender model of your blend file, and thus knows the path to the blend file and since //image01.tif is a reference relative to the blend file, it can construct the UNIX path to the file. You want to know the UNIX pathname to the image file (maybe you want to pass it to some unrelated application to view the file or something). Exactly. I am using Python to log a series of experiments. If I have a record of the settings and the assets used, I can better learn from what works and so repeat the successes and avoid the failures. So you need to do what Blender would do if it needs a UNIX path (eg to open the file). The formula for that is dirname(path_to_blendfile) with n.image.file[2:] appended to it. So that's what we do with unblenderise(): if the filename is a "Blender relative name", rip off the "//" and prepend the blend file directory path. That gets you a UNIX path which you can hand to any function expecting a normal operating system pathname. Whether that is an absolute path or a relative path is entirely "does the resulting path start with a '/'"? We used blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' So, if I read you correctly, this is an absolute path. When we used unblenderise on it, out popped a path that was partly but not wholly relative. The latter half of the path is relative to the absolute first half (/Users/Lion/Desktop/test8/). /Users/Lion/Desktop/test8/../images/image02.tif It starts with a slash but cannot be pasted into "Go to...". But maybe that is an absolute path in UNIX? An absolute path starts with a slash and is relative to the root of the filesystem. You can use such a path regardless of what your current working directory is, because it doesn't use the working directory. A relative path doesn't start with a slash and is relative to the current working directory. It only works if you're in the right working directory. _Because_ a relative path depends on the _your_ working directory, usually we pass around absolute paths if we need to tell something else about a file, because that will work regardless if what _their_ working directory may be. So, you may well want to turn a relative path into an absolute path... If I decided not to supply the path and so ended up with a relative UNIX path, I could now use
Re: absolute path to a file
Please remember to CC the list. On 19Aug2019 22:06, Paul St George wrote: On 19/08/2019 14:16, Cameron Simpson wrote: [...] There's a remark on that web page I mentioned that suggests that the leading '//' indicates the filename is relative to the Blender model, so the context directory for the '//' is likely /Users/Lion/Desktop/test8. Yes. That makes sense. The reason I was testing with two images, one at /Users/Lion/Desktop/test8/image01.tif and the other at /Users/Lion/Desktop/images/image02.tif is that I cannot rely on images being in the same folder as the Blender file. So, let's assume the context directory is /Users/Lion/Desktop/test8 and see how we get on below. [...] realpath needs a UNIX path. Your //image01.tif isn't a UNIX path, it is a special Blender path. First you need to convert it. By replacing '//' with the blend file's directory. Then you can call realpath. If you still need to. Understood. Now. Thanks! [...snip...] Did you just [...snip...] yourself? Yes. It keeps the surrounding context manageable. In this way you know to which text I am referring, without having to wade through paragraphs to guess what may be relevant. from os.path import dirname # Get this from somewhere just hardwiring it for the example. # Maybe from your 'n' object below? blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' Is this setting a relative path? blender_image_file = n.image.filename unix_image_file = unblenderise(blender_image_file, dirname(blend_file)) Now you have a UNIX path. If blend_file is an absolute path, unix_image_path will also be an absolute path. But if blend_file is a relative path (eg you opened up "tifftest8.blend") unix_image_path will be a relative path. Does unix_image_path = unix_image_file? Yeah, sorry, my mistake. Two possibilities here. blend_file (and so unix_image_file) is an absolute path OR blend_file (and so unix_image_file) is a relative path. I just want to check my understanding. If I supply the path to blend_file then it is absolute, and if I ask Python to generate the path to blend_file from within Blender it is relative. Have I got it? Not quite. What seems to be the situation is: You've got some object from Blender called "n.image", which has a ".file" attribute which is a Blender reference to the image file of the form "//image01.tif". I presume that Blender has enough state inside "n" or "n.image" to locate this in the real filesystem; maybe it has some link to the Blender model of your blend file, and thus knows the path to the blend file and since //image01.tif is a reference relative to the blend file, it can construct the UNIX path to the file. You want to know the UNIX pathname to the image file (maybe you want to pass it to some unrelated application to view the file or something). So you need to do what Blender would do if it needs a UNIX path (eg to open the file). The formula for that is dirname(path_to_blendfile) with n.image.file[2:] appended to it. So that's what we do with unblenderise(): if the filename is a "Blender relative name", rip off the "//" and prepend the blend file directory path. That gets you a UNIX path which you can hand to any function expecting a normal operating system pathname. Whether that is an absolute path or a relative path is entirely "does the resulting path start with a '/'"? An absolute path starts with a slash and is relative to the root of the filesystem. You can use such a path regardless of what your current working directory is, because it doesn't use the working directory. A relative path doesn't start with a slash and is relative to the current working directory. It only works if you're in the right working directory. _Because_ a relative path depends on the _your_ working directory, usually we pass around absolute paths if we need to tell something else about a file, because that will work regardless if what _their_ working directory may be. So, you may well want to turn a relative path into an absolute path... If I decided not to supply the path and so ended up with a relative UNIX path, I could now use realpath or abspath to find the absolute path. Have I still got it? This is correct. Abspath may even call realpath to do its work, unsure. It works very well. So thank you! I tested it with a Blend file that had two images, one in the same folder as the Blend file and the other was in a folder on the Desktop called 'images'. The initial results were: Plane uses image01.tif saved at //image01.tif which is at /Users/Lion/Desktop/test8/image01.tif Plane uses image02.tif saved at //../images/image02.tif which is at /Users/Lion/Desktop/test8/../images/image02.tif BUT as you say, this was easily sorted by using os.path.realpath or os.path.abspath. Both worked equally well. Yep. Abspath does some things in a purely lexical way: it resolves '.' and '..' components in the path even if they
Re: absolute path to a file
On 19/08/2019 14:16, Cameron Simpson wrote: On 19Aug2019 08:52, Paul St George wrote: On 19/08/2019 01:31, Cameron Simpson wrote: On 18Aug2019 17:29, Paul St George wrote: On 18/08/2019 02:03, Cameron Simpson wrote: 1: Is image01.tif a real existing file when you ran this code? Yes. image01.tif is real, existing and apparent. But in what directory? What is its _actual_ full path as you expect it to be? Aha. The Blender file that I am using to test this on has two images used as textures. They reside at: /Users/Lion/Desktop/test8/image01.tif /Users/Lion/Desktop/images/image02.tif The Blender file is at /Users/Lion/Desktop/test8/tifftest8.blend There's a remark on that web page I mentioned that suggests that the leading '//' indicates the filename is relative to the Blender model, so the context directory for the '//' is likely /Users/Lion/Desktop/test8. Yes. That makes sense. The reason I was testing with two images, one at /Users/Lion/Desktop/test8/image01.tif and the other at /Users/Lion/Desktop/images/image02.tif is that I cannot rely on images being in the same folder as the Blender file. So, let's assume the context directory is /Users/Lion/Desktop/test8 and see how we get on below. (Chris and Peter lead me to believe that Blender has a special kind of relative path. The double slashes replace the path to the blend file’s directory.) realpath has done something but I know not what. realpath needs a UNIX path. Your //image01.tif isn't a UNIX path, it is a special Blender path. First you need to convert it. By replacing '//' with the blend file's directory. Then you can call realpath. If you still need to. Understood. Now. Thanks! [...snip...] Did you just [...snip...] yourself? So you might want to write an unBlenderiser (untested example): from os.path import isabs, join as joinpath def unblenderise(filename, context_dir=None): # transmute Blender "relative" path if filename.startswith('//'): filename = filename[2:] if not isabs(filename) and context_dir is not None: # attach the filename to `context_dir` if supplied filename = joinpath(context_dir, filename) return filename The idea here is to get back a meaningful UNIX path from a Blender path. It first strips a leading '//'. Next, _if_ the filename is relative _and_ you supplied the optional context_dir (eg the directory used for a file brwoser dialogue box), then it prepends that context directory. This _does not_ call abspath or realpath or anything, it just returns a filename which can be used with them. The idea is that if you know where image01.tif lives, you could supply that as the context directory. Yes! Until Peter Otten's timely intervention, I was trying to do this and had managed to join the path_to_the_folder_that_contains_the_Blender_file to the_name_of_the_image (stripped of its preceding slashes). Your unblenderise looks much better than my nascent saneblender so thank you. I will explore more! Looks like you want wd=os.path.dirname(path_of_blend_file). Thank you! Does it depend on knowing where image01.tif lives and manually supplying that? Sort of. What you've got is '//image01.tif', which is Blender's special notation indicating a filename relative to the blend file's directory. All the Python library stuff expects an OS path (OS==UNIX on a Mac). So you want to convert that into a UNIX path. For example: from os.path import dirname # Get this from somewhere just hardwiring it for the example. # Maybe from your 'n' object below? blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' Is this setting a relative path? blender_image_file = n.image.filename unix_image_file = unblenderise(blender_image_file, dirname(blend_file)) Now you have a UNIX path. If blend_file is an absolute path, unix_image_path will also be an absolute path. But if blend_file is a relative path (eg you opened up "tifftest8.blend") unix_image_path will be a relative path. Does unix_image_path = unix_image_file? Two possibilities here. blend_file (and so unix_image_file) is an absolute path OR blend_file (and so unix_image_file) is a relative path. I just want to check my understanding. If I supply the path to blend_file then it is absolute, and if I ask Python to generate the path to blend_file from within Blender it is relative. Have I got it? You don't need to use realpath or abspath on that _unless_ you need to reference the file from any directory (i.e. _not_ relative to where you currently are). If I decided not to supply the path and so ended up with a relative UNIX path, I could now use realpath or abspath to find the absolute path. Have I still got it? # I combined your unblenderise and your use of it (see below). Is my attempt error free? It works very well. So thank you! I tested it with a Blend file that had two images, one in the same folder as the Blend file and the other was in a
Re: absolute path to a file
On 19Aug2019 08:52, Paul St George wrote: On 19/08/2019 01:31, Cameron Simpson wrote: On 18Aug2019 17:29, Paul St George wrote: On 18/08/2019 02:03, Cameron Simpson wrote: 1: Is image01.tif a real existing file when you ran this code? Yes. image01.tif is real, existing and apparent. But in what directory? What is its _actual_ full path as you expect it to be? Aha. The Blender file that I am using to test this on has two images used as textures. They reside at: /Users/Lion/Desktop/test8/image01.tif /Users/Lion/Desktop/images/image02.tif The Blender file is at /Users/Lion/Desktop/test8/tifftest8.blend There's a remark on that web page I mentioned that suggests that the leading '//' indicates the filename is relative to the Blender model, so the context directory for the '//' is likely /Users/Lion/Desktop/test8. (Chris and Peter lead me to believe that Blender has a special kind of relative path. The double slashes replace the path to the blend file’s directory.) realpath has done something but I know not what. realpath needs a UNIX path. Your //image01.tif isn't a UNIX path, it is a special Blender path. First you need to convert it. By replacing '//' with the blend file's directory. Then you can call realpath. If you still need to. [...snip...] So you might want to write an unBlenderiser (untested example): from os.path import isabs, join as joinpath def unblenderise(filename, context_dir=None): # transmute Blender "relative" path if filename.startswith('//'): filename = filename[2:] if not isabs(filename) and context_dir is not None: # attach the filename to `context_dir` if supplied filename = joinpath(context_dir, filename) return filename The idea here is to get back a meaningful UNIX path from a Blender path. It first strips a leading '//'. Next, _if_ the filename is relative _and_ you supplied the optional context_dir (eg the directory used for a file brwoser dialogue box), then it prepends that context directory. This _does not_ call abspath or realpath or anything, it just returns a filename which can be used with them. The idea is that if you know where image01.tif lives, you could supply that as the context directory. Yes! Until Peter Otten's timely intervention, I was trying to do this and had managed to join the path_to_the_folder_that_contains_the_Blender_file to the_name_of_the_image (stripped of its preceding slashes). Your unblenderise looks much better than my nascent saneblender so thank you. I will explore more! Looks like you want wd=os.path.dirname(path_of_blend_file). Does it depend on knowing where image01.tif lives and manually supplying that? Sort of. What you've got is '//image01.tif', which is Blender's special notation indicating a filename relative to the blend file's directory. All the Python library stuff expects an OS path (OS==UNIX on a Mac). So you want to convert that into a UNIX path. For example: from os.path import dirname # Get this from somewhere just hardwiring it for the example. # Maybe from your 'n' object below? blend_file = '/Users/Lion/Desktop/test8/tifftest8.blend' blender_image_file = n.image.filename unix_image_file = unblenderise(blender_image_file, dirname(blend_file)) Now you have a UNIX path. If blend_file is an absolute path, unix_image_path will also be an absolute path. But if blend_file is a relative path (eg you opened up "tifftest8.blend") unix_image_path will be a relative path. You don't need to use realpath or abspath on that _unless_ you need to reference the file from any directory (i.e. _not_ relative to where you currently are). Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 19/08/2019 01:31, Cameron Simpson wrote: Paul, I can see we must train you in the interleaved response style :-) On 18Aug2019 17:29, Paul St George wrote: On 18/08/2019 02:03, Cameron Simpson wrote: 1: Is image01.tif a real existing file when you ran this code? Yes. image01.tif is real, existing and apparent. But in what directory? What is its _actual_ full path as you expect it to be? Aha. The Blender file that I am using to test this on has two images used as textures. They reside at: /Users/Lion/Desktop/test8/image01.tif /Users/Lion/Desktop/images/image02.tif The Blender file is at /Users/Lion/Desktop/test8/tifftest8.blend print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which is at', realpath(n.image.filepath)) gives: Plane uses image01.tif saved at //image01.tif which is at /image01.tif (Chris and Peter lead me to believe that Blender has a special kind of relative path. The double slashes replace the path to the blend file’s directory.) realpath has done something but I know not what. I expect that realpath has normalised the string '//image01.tif' into the normal UNIX path '/image01.tif'. Because realpath works with UNIX paths, '//image01.tif' is not the path to an existing file from its point of view, because that would be in / ('//' is equivalent to '/' in UNIX, because multiple slashes coalesce). 2a: What is your current working directory when you run this code? Well, there at least two answers to that question. Within Blender's Python Console: os.getcwd() ‘/‘ Did you run the other code in Blender's Console? I ran this from Blender's console: >>>filename = "/Users/Lion/Desktop/test8/readout.py" >>>exec(compile(open(filename).read(), filename, 'exec')) I ran os.getcwd() from both within /Users/Lion/Desktop/test8/readout.py and directly within the Blender console. >> ‘/‘ The thing to bear in mind is that on many GUI desktops a GUI application's working directory may be '/'. Specificly, _not_ your home directory. This is because they're invoked (usually) by the desktop manager programme. And also because it sidesteps awkward situations like having the working directory in some removable medium such as a thumb drive, which might get removed. So the Blender CWD is _not_ your home directory or whatever directory your terminal prompt may be in. And therefore things that depend on the current directory will behave counterintuitively. In your case, by getting '/' from os.getcwd(). So: if the image01.tif file is _not_ in '/' (which I expect to be the case) then Python's realpath will be doing a lexical operation because as far as it is concerned the pathname ('//image01.tif') is not the path of an existing file. And its getcwd() isn't what you expect, thus the result not being what you want. But I am *guessing* the real location of the CWD is /Applications/Blender/blender.app/Contents/Resources/2.79/scripts This isn't a meaningful statement to me. The CWD is the working directory of the python interpreter running your code. If that is the Blender Console and the consale says the cwd is '/', then that's what it is. In particular, if you invoke some Python script as /path/to/script.py, your cwd does not magicly become '/path/to'. The cwd is a per process ephemeral thing, which is why the shell "cd" command works: it switches the working directory for the shell, and that context is inherited by child processes (your commands). I tried using realpath, bpy.path.abspath, and os.path.abspath on this forward slash but nothing changed. Because the leading slash makes it an absolute path by UNIX rules. The cwd never gets a look in; it is irrelevant. For amusement, I also tried print(os.path.join(os.getcwd(), os.getcwd())) os.path.join is pretty lexical; it largely exists to avoid wiring in an OS-dependent path separator into your code (eg Windows' '\\' versus UNIX '/' or OS9 ':'). But it notices an absolute path in the sequence and discards earlier values, so the second getcwd is kept; not sure why this is useful behaviour. But putting an absolute path on the right hand end of a join isn't meaningful either, so the result is at best amusing anyway. 2b: If the underlying OS realpath fails, the Python library function might fall back on os.path.join(os.getcwd(), filename). It should, shouldn’t it. But it doesn’t. Well, I over simplified. If your path doesn't stat() (== "resolve directly to a filesystem object", on which the OS realpath relies), Python's realpath seems to be doing a _purely lexical_ path normalisation, like os.path.normpath. So, what have you passed in? '//image01.tif'. That is an absolute path. The cwd is not relevant, realpath runs with it as is. 'image01.tif' is not in '/', so it doesn't stat(), so we just normalise the path to '/image01.tif'. Alternative values: 'image01.tif' (your n.image.filepath[2:], to skip the Blender specific '//' relative pathname notation).
Re: absolute path to a file
Paul, I can see we must train you in the interleaved response style :-) On 18Aug2019 17:29, Paul St George wrote: On 18/08/2019 02:03, Cameron Simpson wrote: 1: Is image01.tif a real existing file when you ran this code? Yes. image01.tif is real, existing and apparent. But in what directory? What is its _actual_ full path as you expect it to be? print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which is at', realpath(n.image.filepath)) gives: Plane uses image01.tif saved at //image01.tif which is at /image01.tif (Chris and Peter lead me to believe that Blender has a special kind of relative path. The double slashes replace the path to the blend file’s directory.) realpath has done something but I know not what. I expect that realpath has normalised the string '//image01.tif' into the normal UNIX path '/image01.tif'. Because realpath works with UNIX paths, '//image01.tif' is not the path to an existing file from its point of view, because that would be in / ('//' is equivalent to '/' in UNIX, because multiple slashes coalesce). 2a: What is your current working directory when you run this code? Well, there at least two answers to that question. Within Blender's Python Console: os.getcwd() ‘/‘ Did you run the other code in Blender's Console? The thing to bear in mind is that on many GUI desktops a GUI application's working directory may be '/'. Specificly, _not_ your home directory. This is because they're invoked (usually) by the desktop manager programme. And also because it sidesteps awkward situations like having the working directory in some removable medium such as a thumb drive, which might get removed. So the Blender CWD is _not_ your home directory or whatever directory your terminal prompt may be in. And therefore things that depend on the current directory will behave counterintuitively. In your case, by getting '/' from os.getcwd(). So: if the image01.tif file is _not_ in '/' (which I expect to be the case) then Python's realpath will be doing a lexical operation because as far as it is concerned the pathname ('//image01.tif') is not the path of an existing file. And its getcwd() isn't what you expect, thus the result not being what you want. But I am *guessing* the real location of the CWD is /Applications/Blender/blender.app/Contents/Resources/2.79/scripts This isn't a meaningful statement to me. The CWD is the working directory of the python interpreter running your code. If that is the Blender Console and the consale says the cwd is '/', then that's what it is. In particular, if you invoke some Python script as /path/to/script.py, your cwd does not magicly become '/path/to'. The cwd is a per process ephemeral thing, which is why the shell "cd" command works: it switches the working directory for the shell, and that context is inherited by child processes (your commands). I tried using realpath, bpy.path.abspath, and os.path.abspath on this forward slash but nothing changed. Because the leading slash makes it an absolute path by UNIX rules. The cwd never gets a look in; it is irrelevant. For amusement, I also tried print(os.path.join(os.getcwd(), os.getcwd())) os.path.join is pretty lexical; it largely exists to avoid wiring in an OS-dependent path separator into your code (eg Windows' '\\' versus UNIX '/' or OS9 ':'). But it notices an absolute path in the sequence and discards earlier values, so the second getcwd is kept; not sure why this is useful behaviour. But putting an absolute path on the right hand end of a join isn't meaningful either, so the result is at best amusing anyway. 2b: If the underlying OS realpath fails, the Python library function might fall back on os.path.join(os.getcwd(), filename). It should, shouldn’t it. But it doesn’t. Well, I over simplified. If your path doesn't stat() (== "resolve directly to a filesystem object", on which the OS realpath relies), Python's realpath seems to be doing a _purely lexical_ path normalisation, like os.path.normpath. So, what have you passed in? '//image01.tif'. That is an absolute path. The cwd is not relevant, realpath runs with it as is. 'image01.tif' is not in '/', so it doesn't stat(), so we just normalise the path to '/image01.tif'. Alternative values: 'image01.tif' (your n.image.filepath[2:], to skip the Blender specific '//' relative pathname notation). That is a relative path. Can we stat it? That will happen in the Python interpreter's cwd, which apparently is Blender's cwd, which is '/'. And there's no '/image01.tif', so it doesn't stat. Back to lexcial work: normpath(os.path.join(os.getcwd(),filename)) i.e.: normpath(os.path.joinpath('/','image01.tif')) which becomes '/image01.tif'. Anyway, wouldn’t this be an absolute path via the location of the CWD? That's not a meaningful term to me. An absolute path begins with a slash and doesn't depend on the cwd to locate the filesystem object. A
Re: absolute path to a file
On 18/08/2019 02:03, Cameron Simpson wrote: On 17Aug2019 11:51, Paul St George wrote: print('Track D from Track B:',os.path.realpath(n.image.filepath)) ---Track D from Track B: /image01.tif print('Track E from Track B:',os.path.realpath(n.image.filepath[1:])) ---Track E from Track B: /image01.tif print('Track F from Track C:',os.path.realpath(n.image.filepath[2:])) ---Track F from Track C: /image01.tif I know these may just be cut/paste, but you can shorten this by: from os.path import realpath print('Track F from Track C:',realpath(n.image.filepath[2:])) I have 2 other questions: 1: Is image01.tif a real existing file when you ran this code? realpath kind of expects that - it may do nothing for something which doesn't exist. I'd point out that realpath(3), which you get from the command "man 3 realpath", says: The realpath() function will resolve both absolute and relative paths and return the absolute pathname corresponding to file_name. All components of file_name must exist when realpath() is called. Most of the things in the os module are just thin wrappers for the OS supplied functions, which is why the MacOS realpath library function is relevant. 2: What is your current working directory when you run this code? If the underlying OS realpath fails, the Python library function might fall back on os.path.join(os.getcwd(), filename). 3: What is the airspeed velocity of an unladen swallow? https://www.python.org/dev/peps/pep-0234/#rationale Cheers, Cameron Simpson (formerly c...@zip.com.au) 1: Is image01.tif a real existing file when you ran this code? Yes. image01.tif is real, existing and apparent. >>> print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which is at', realpath(n.image.filepath)) gives: Plane uses image01.tif saved at //image01.tif which is at /image01.tif (Chris and Peter lead me to believe that Blender has a special kind of relative path. The double slashes replace the path to the blend file’s directory.) realpath has done something but I know not what. 2a: What is your current working directory when you run this code? Well, there at least two answers to that question. Within Blender's Python Console: >>> os.getcwd() ‘/‘ But I am *guessing* the real location of the CWD is /Applications/Blender/blender.app/Contents/Resources/2.79/scripts I tried using realpath, bpy.path.abspath, and os.path.abspath on this forward slash but nothing changed. For amusement, I also tried print(os.path.join(os.getcwd(), os.getcwd())) 2b: If the underlying OS realpath fails, the Python library function might fall back on os.path.join(os.getcwd(), filename). It should, shouldn’t it. But it doesn’t. Anyway, wouldn’t this be an absolute path via the location of the CWD? 3: What is the airspeed velocity of an unladen swallow? Do you know the speed and direction of the swallow? -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 17Aug2019 11:51, Paul St George wrote: print('Track D from Track B:',os.path.realpath(n.image.filepath)) ---Track D from Track B: /image01.tif print('Track E from Track B:',os.path.realpath(n.image.filepath[1:])) ---Track E from Track B: /image01.tif print('Track F from Track C:',os.path.realpath(n.image.filepath[2:])) ---Track F from Track C: /image01.tif I know these may just be cut/paste, but you can shorten this by: from os.path import realpath print('Track F from Track C:',realpath(n.image.filepath[2:])) I have 2 other questions: 1: Is image01.tif a real existing file when you ran this code? realpath kind of expects that - it may do nothing for something which doesn't exist. I'd point out that realpath(3), which you get from the command "man 3 realpath", says: The realpath() function will resolve both absolute and relative paths and return the absolute pathname corresponding to file_name. All components of file_name must exist when realpath() is called. Most of the things in the os module are just thin wrappers for the OS supplied functions, which is why the MacOS realpath library function is relevant. 2: What is your current working directory when you run this code? If the underlying OS realpath fails, the Python library function might fall back on os.path.join(os.getcwd(), filename). 3: What is the airspeed velocity of an unladen swallow? https://www.python.org/dev/peps/pep-0234/#rationale Cheers, Cameron Simpson (formerly c...@zip.com.au) -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 17/08/2019 15:37, Peter Otten wrote: Paul St George wrote: Can someone please tell me how to get the absolute path to a file? I have tried os.path.abspath. In the code below I have a problem in the final line (15). # |import bpy|| Is this blender? If so the "//" prefix starts making sense: https://docs.blender.org/api/current/bpy.path.html I'd try bpy.path.abspath() instead of os.pathabspath(). ||import os|| || ||texture_list = []|| || ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as outstream:|| || || ||for obj in bpy.context.scene.objects:|| ||for s in obj.material_slots:|| ||if s.material and s.material.use_nodes:|| ||for n in s.material.node_tree.nodes:|| ||if n.type == 'TEX_IMAGE':|| ||texture_list += [n.image]|| ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which is at', os.path.abspath(n.image.filepath), file=outstream)|| |# This gives me: ---Plane uses image01.tif saved at //image01.tif which is at //image01.tif ---Plane uses image02.tif saved at //../images/image02.tif which is at //images/image02.tif But I want an absolute path such as: ---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif ---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif If it is relevant, my files are on a Mac. Hence the escaped forward slash. Now following Peter's immensely useful nudge towards bpy.path.abspath(), I have learned that Blender uses a special kind of relative path. These paths start with ”//” . The double slashes replace the path to the blend file’s directory. This means that py.path.abspath() gives a path that is absolute for most of its length then relative towards the end. The good news is that I have found a way to tidy this. os.path.abspath(bpy.path.abspath(p)) This gives a fully absolute file path. The bpy.path.abspath function takes care of the ’//’ replacing it with the full path to the file, while os.path.abspath takes care of any other unwelcome relative ’../’ that might remain. Thanks to everyone for their help! Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 8/17/19 10:21 AM, Paul St George wrote: > Yes, it is Blender and the bpy.path.abspath() works! > And thank you also for the link to the docs. They say: > Returns the absolute path relative to the current blend file using the > “//” prefix. > > So does Blender have its own Python??? I don't think it has its own Python, but it sounds like it sort of extends the file system, and uses a leading // for something special. That means files that begin with // need to be processes through the blender library and not be used directly to the OS. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 17/08/2019 16:32, Dennis Lee Bieber wrote: On Sat, 17 Aug 2019 11:51:47 +0200, Paul St George declaimed the following: print('Track E from Track B:',os.path.realpath(n.image.filepath[1:])) ---Track E from Track B: /image01.tif Just for giggles, what happens if you preface that with a single period... print('Track E from Track B:',os.path.realpath("." + n.image.filepath[1:])) print('Track E for giggles from Track B:',os.path.realpath("." + n.image.filepath[1:])) gives: Track E for giggles from Track B: /images/blah.tif -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 17/08/2019 15:37, Peter Otten wrote: Paul St George wrote: Can someone please tell me how to get the absolute path to a file? I have tried os.path.abspath. In the code below I have a problem in the final line (15). # |import bpy|| Is this blender? If so the "//" prefix starts making sense: https://docs.blender.org/api/current/bpy.path.html I'd try bpy.path.abspath() instead of os.pathabspath(). ||import os|| || ||texture_list = []|| || ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as outstream:|| || || ||for obj in bpy.context.scene.objects:|| ||for s in obj.material_slots:|| ||if s.material and s.material.use_nodes:|| ||for n in s.material.node_tree.nodes:|| ||if n.type == 'TEX_IMAGE':|| ||texture_list += [n.image]|| ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which is at', os.path.abspath(n.image.filepath), file=outstream)|| |# This gives me: ---Plane uses image01.tif saved at //image01.tif which is at //image01.tif ---Plane uses image02.tif saved at //../images/image02.tif which is at //images/image02.tif But I want an absolute path such as: ---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif ---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif If it is relevant, my files are on a Mac. Hence the escaped forward slash. Yes, it is Blender and the bpy.path.abspath() works! And thank you also for the link to the docs. They say: Returns the absolute path relative to the current blend file using the “//” prefix. So does Blender have its own Python??? -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
Paul St George wrote: > Can someone please tell me how to get the absolute path to a file? I > have tried os.path.abspath. In the code below I have a problem in the > final line (15). > > # > |import bpy|| Is this blender? If so the "//" prefix starts making sense: https://docs.blender.org/api/current/bpy.path.html I'd try bpy.path.abspath() instead of os.pathabspath(). > ||import os|| > || > ||texture_list = []|| > || > ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as > outstream:|| > || > || > ||for obj in bpy.context.scene.objects:|| > ||for s in obj.material_slots:|| > ||if s.material and s.material.use_nodes:|| > ||for n in s.material.node_tree.nodes:|| > ||if n.type == 'TEX_IMAGE':|| > ||texture_list += [n.image]|| > ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which > is at', os.path.abspath(n.image.filepath), file=outstream)|| > |# > > This gives me: > ---Plane uses image01.tif saved at //image01.tif which is at //image01.tif > ---Plane uses image02.tif saved at //../images/image02.tif which is at > //images/image02.tif > > But I want an absolute path such as: > ---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif > ---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif > > If it is relevant, my files are on a Mac. Hence the escaped forward slash. > -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
Hi Paul, Here an example how I used both functions https://gitlab.com/snippets/1886520 Hope this helps. -- Manfred -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 17/08/2019 01:07, Gregory Ewing wrote: On Sat, Aug 17, 2019 at 2:27 AM Paul St George wrote: BUT does not work with | print('test2:',os.path.realpath(n.image.filepath))| This returns only |/image01.tif| What does n.image.filepath look like on its own? If it starts with a leading slash, then os.path.realpath will think it's already an absolute path and leave it alone. The problem then is why it's getting a leading slash in the first place. It looks like the result of something naively trying to append a filename to an empty directory path. Gregory, Thank you for this. You ask what n.image.filepath looks like on its own. Here (below) are some tests and the results: You are right about the leading slashes. I have tried to remove them and then use os.path.realpath and os.path.abspath but it is as if the slash is still there. Perhaps there is a better way to remove the slash - perhaps by getting the name of the file. Then I could find the true path to the directory that contains the file, and finally join the two together. But there must be a simpler way? print('Track A:',n.image.filepath) ---Track A: //image01.tif print('Track B:',n.image.filepath[1:]) ---Track B: /image01.tif print('Track C:',n.image.filepath[2:]) ---Track C: image01.tif print('Track D from Track B:',os.path.realpath(n.image.filepath)) ---Track D from Track B: /image01.tif print('Track E from Track B:',os.path.realpath(n.image.filepath[1:])) ---Track E from Track B: /image01.tif print('Track F from Track C:',os.path.realpath(n.image.filepath[2:])) ---Track F from Track C: /image01.tif print('Track G from Track C:',os.path.abspath(n.image.filepath[2:])) ---Track G from Track C: /image01.tif -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On Sat, Aug 17, 2019 at 2:27 AM Paul St George wrote: BUT does not work with | print('test2:',os.path.realpath(n.image.filepath))| This returns only |/image01.tif| What does n.image.filepath look like on its own? If it starts with a leading slash, then os.path.realpath will think it's already an absolute path and leave it alone. The problem then is why it's getting a leading slash in the first place. It looks like the result of something naively trying to append a filename to an empty directory path. -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On Sat, Aug 17, 2019 at 5:28 AM Paul St George wrote: > > On 16/08/2019 18:37, Chris Angelico wrote: > > On Sat, Aug 17, 2019 at 2:27 AM Paul St George > > wrote: > >> BUT does not work with > >> | print('test2:',os.path.realpath(n.image.filepath))| > >> > >> This returns only > >> |/image01.tif| > >> > >> > >> Notes: > >> Chris, I only mention the extra leading slash on my Mac in case anyone > >> wonders why it is there. Python puts it there to escape the following > >> slash. > > > > I still don't understand what you mean by that, because there's no > > concept of "escaping" with these slashes. It looks like you're > > actually working with absolute paths (starting with the leading slash) > > when you want to work with relative paths (NOT starting with a leading > > slash). The double slash isn't "escaping" anything, to my knowledge, > > and Python would not add it. > > > > From the look of things, you really are getting a valid absolute path > > - "/image01.tif" is already absolute. It just isn't the path you want. > > > > ChrisA > > > > Yes, perhaps I am using the wrong terms. I want to find the path that > looks like this: > /Users/Lion/Desktop/test8/image01.tif > > With such a path, I can find the image file. I cannot find the file with > only /image01.tif > > It is safe to ignore what I said about the double forward slashes. I am > not using these. I only observed their presence and made a guess at > their meaning. > When your path starts with "/Users", that means the Users directory which is found in the root directory of your hard drive. When your path starts with "/image01.tif", that means that image01.tif needs to be in the root directory. If that's not the case (which I highly suspect), then you do NOT want a leading slash - just use "image01.tif", which means the file should be in the *current* directory. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 16/08/2019 18:37, Chris Angelico wrote: On Sat, Aug 17, 2019 at 2:27 AM Paul St George wrote: BUT does not work with | print('test2:',os.path.realpath(n.image.filepath))| This returns only |/image01.tif| Notes: Chris, I only mention the extra leading slash on my Mac in case anyone wonders why it is there. Python puts it there to escape the following slash. I still don't understand what you mean by that, because there's no concept of "escaping" with these slashes. It looks like you're actually working with absolute paths (starting with the leading slash) when you want to work with relative paths (NOT starting with a leading slash). The double slash isn't "escaping" anything, to my knowledge, and Python would not add it. From the look of things, you really are getting a valid absolute path - "/image01.tif" is already absolute. It just isn't the path you want. ChrisA Yes, perhaps I am using the wrong terms. I want to find the path that looks like this: /Users/Lion/Desktop/test8/image01.tif With such a path, I can find the image file. I cannot find the file with only /image01.tif It is safe to ignore what I said about the double forward slashes. I am not using these. I only observed their presence and made a guess at their meaning. Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: Re: absolute path to a file
On Sat, Aug 17, 2019 at 2:27 AM Paul St George wrote: > BUT does not work with > | print('test2:',os.path.realpath(n.image.filepath))| > > This returns only > |/image01.tif| > > > Notes: > Chris, I only mention the extra leading slash on my Mac in case anyone > wonders why it is there. Python puts it there to escape the following slash. I still don't understand what you mean by that, because there's no concept of "escaping" with these slashes. It looks like you're actually working with absolute paths (starting with the leading slash) when you want to work with relative paths (NOT starting with a leading slash). The double slash isn't "escaping" anything, to my knowledge, and Python would not add it. >From the look of things, you really are getting a valid absolute path - "/image01.tif" is already absolute. It just isn't the path you want. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Re: absolute path to a file
Thank you Manfred and Cameron! I think the problem may lie within syntax rather than vocabulary. The code works in one place but not where I use it in my script*. Cameron’s suggestion works when I try | print('test1:', os.path.realpath(bpy.data.filepath))| This returns: |/Users/Lion/Desktop/test8/tifftest8.blend| BUT does not work with | print('test2:',os.path.realpath(n.image.filepath))| This returns only |/image01.tif| Here is my full script: # starts |import bpy|| ||import os|| ||from pathlib import Path || || ||texture_list = []|| || ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as outstream:|| || || || for obj in bpy.context.scene.objects:|| || for s in obj.material_slots:|| || if s.material and s.material.use_nodes:|| || for n in s.material.node_tree.nodes:|| || if n.type == 'TEX_IMAGE':|| || texture_list += [n.image]|| ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which is at', os.path.realpath(n.image.filepath), file=outstream)|| |# ends Notes: Chris, I only mention the extra leading slash on my Mac in case anyone wonders why it is there. Python puts it there to escape the following slash. Perhaps I should also mention (in case it is relevant) that I am calling my script ‘texturescript.py’ from the Python console of Blender. I use: |filename = "/Users/Lion/Desktop/test8/texturescript.py"|| ||exec(compile(open(filename).read(), filename, 'exec'))| * My original |os.path.abspath| also works (and doesn’t work) in these circumstances. As does Manfred’s: |Path('./myfile').resolve()|. On 16/08/2019 05:44, Manfred Lotz wrote: On Fri, 16 Aug 2019 09:00:38 +1000 Cameron Simpson wrote: On 15Aug2019 22:52, Manfred Lotz wrote: I did this: >from pathlib import Path abs_myfile = Path('./myfile').resolve() which worked fine for me. There is also os.path.realpath(filename) for this purpose. In modern Python that also accepts a Pathlike object. Thanks for this. I changed my code to use your suggestion which seems to be better for the situation where I used resolve() before. -- Paul St George http://www.paulstgeorge.com http://www.devices-of-wonder.com +44(0)7595 37 1302 -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On Fri, 16 Aug 2019 09:00:38 +1000 Cameron Simpson wrote: > On 15Aug2019 22:52, Manfred Lotz wrote: > >I did this: > >from pathlib import Path > >abs_myfile = Path('./myfile').resolve() > >which worked fine for me. > > There is also os.path.realpath(filename) for this purpose. In modern > Python that also accepts a Pathlike object. Thanks for this. I changed my code to use your suggestion which seems to be better for the situation where I used resolve() before. -- Manfred -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On 15Aug2019 22:52, Manfred Lotz wrote: On Thu, 15 Aug 2019 22:00:17 +0200 Paul St George wrote: But I want an absolute path such as: ---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif ---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif If it is relevant, my files are on a Mac. Hence the escaped forward slash. You don't need to double the leading slash on a Mac. On 15Aug2019 22:52, Manfred Lotz wrote: I did this: from pathlib import Path abs_myfile = Path('./myfile').resolve() which worked fine for me. There is also os.path.realpath(filename) for this purpose. In modern Python that also accepts a Pathlike object. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On Thu, 15 Aug 2019 22:00:17 +0200 Paul St George wrote: > Can someone please tell me how to get the absolute path to a file? I > have tried os.path.abspath. In the code below I have a problem in the > final line (15). > > # > |import bpy|| > ||import os|| > || > ||texture_list = []|| > || > ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as > outstream:|| > || > || > || for obj in bpy.context.scene.objects:|| > || for s in obj.material_slots:|| > || if s.material and s.material.use_nodes:|| > || for n in s.material.node_tree.nodes:|| > || if n.type == 'TEX_IMAGE':|| > || texture_list += [n.image]|| > ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, > 'which is at', os.path.abspath(n.image.filepath), file=outstream)|| > |# > > This gives me: > ---Plane uses image01.tif saved at //image01.tif which is > at //image01.tif ---Plane uses image02.tif saved > at //../images/image02.tif which is at //images/image02.tif > > But I want an absolute path such as: > ---Plane uses image01.tif saved > at /Users/Lion/Desktop/test8/image01.tif ---Plane uses image02.tif > saved at /Users/Lion/Desktop/images/image02.tif > > If it is relevant, my files are on a Mac. Hence the escaped forward > slash. > I did this: from pathlib import Path abs_myfile = Path('./myfile').resolve() which worked fine for me. -- Manfred -- https://mail.python.org/mailman/listinfo/python-list
Re: absolute path to a file
On Fri, Aug 16, 2019 at 6:01 AM Paul St George wrote: > > Can someone please tell me how to get the absolute path to a file? I > have tried os.path.abspath. In the code below I have a problem in the > final line (15). > > # > |import bpy|| > ||import os|| > || > ||texture_list = []|| > || > ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as > outstream:|| > || > || > || for obj in bpy.context.scene.objects:|| > ||for s in obj.material_slots:|| > ||if s.material and s.material.use_nodes:|| > ||for n in s.material.node_tree.nodes:|| > ||if n.type == 'TEX_IMAGE':|| > ||texture_list += [n.image]|| > ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which > is at', os.path.abspath(n.image.filepath), file=outstream)|| > |# > > This gives me: > ---Plane uses image01.tif saved at //image01.tif which is at //image01.tif > ---Plane uses image02.tif saved at //../images/image02.tif which is at > //images/image02.tif > > But I want an absolute path such as: > ---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif > ---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif > > If it is relevant, my files are on a Mac. Hence the escaped forward slash. > I don't understand the meaning of "escaped forward slash". What does that mean, and how does it affect your path names? -- not a Mac person -- ChrisA -- https://mail.python.org/mailman/listinfo/python-list