pathlib

Barry Scott barry at barrys-emacs.org
Mon Sep 30 04:28:54 EDT 2019



> On 30 Sep 2019, at 05:40, DL Neil via Python-list <python-list at python.org> wrote:
> 
> Should pathlib reflect changes it has made to the file-system?

I think it should not.

A Path() is the name of a file it is not the file itself. Why should it
track changes in the file system for the name?

Here is code to show why following the rename will break logic:

save_file = pathlib.Path('my-data.txt')

def save( data ):
	# backup file
	if save_file.exists():
		save_file.rename('my-data.backup')

	# save data
	with save_file.open() as f:
		f.write( data )

while True:
	save( generate_data() )
	time.sleep( interval )

If the rename of the file changed the path the above code will fail.

Barry


> 
> 
> Sample code, below, shows pathlib identifying a data-file and then renaming it. Yet, after the rename operation, pathlib doesn't recognise its own change; whereas the file system does/proves the change was actioned.
> 
> 
> $ touch before.file
> $ python3
> Python 3.7.4 (default, Jul  9 2019, 16:48:28)
> [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import pathlib
> >>> p = pathlib.Path( "before.file" )
> >>> p
> PosixPath('before.file')
> >>> p.name
> 'before.file'
> >>> p.rename( "after.file" )
> >>> p.name
> 'before.file'
> >>> exit()
> $ ls -lah after*
> -rw-rw-r--. 1 dn dn 0 Sep 30 16:05 after.file
> $ ls -lah before*
> ls: cannot access 'before*': No such file or directory
> 
> 
> I would expect pathlib to keep track of changes it has made. Any ideas/good reasons why/why not?
> NB "name" is a r/o attribute.
> -- 
> Regards,
> =dn
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list