In [1]:
import util_make_files
util_make_files.pathlib_basic()
In [2]:
import pathlib
In [3]:
p_file = pathlib.Path('temp/file.txt')
In [4]:
print(p_file)
In [5]:
print(type(p_file))
In [6]:
print(str(p_file))
In [7]:
print(type(str(p_file)))
In [8]:
print(p_file.name)
In [9]:
print(type(p_file.name))
In [10]:
print(p_file.stem)
In [11]:
print(type(p_file.stem))
In [12]:
p_dir = pathlib.Path('temp/dir/')
In [13]:
print(p_dir)
In [14]:
print(type(p_dir))
In [15]:
print(p_dir.name)
In [16]:
print(p_dir.stem)
In [17]:
print(p_file.suffix)
In [18]:
print(type(p_file.suffix))
In [19]:
print(p_dir.suffix)
In [20]:
print(p_file.suffix.lstrip('.'))
In [21]:
print(p_file.suffix[1:])
In [22]:
print(p_dir.suffix.lstrip('.'))
In [23]:
print(p_dir.suffix[1:])
In [24]:
p_sub = pathlib.Path('temp/dir/sub_dir/file2.txt')
In [25]:
print(p_sub)
In [26]:
print(p_sub.parent)
In [27]:
print(type(p_sub.parent))
In [28]:
print(p_sub.parents[0])
In [29]:
print(p_sub.parents[1])
In [30]:
print(p_sub.parents[2])
In [31]:
print(p_sub.parents[3])
In [32]:
# print(p_sub.parents[4])
# IndexError: 4
In [33]:
p_abs = p_sub.resolve()
In [34]:
print(p_abs)
In [35]:
print(p_abs.parents[4])
In [36]:
# print(p_abs.parents[10])
# IndexError: 10
In [37]:
p_file = pathlib.Path('temp/file.txt')
In [38]:
print(p_file)
In [39]:
p_file_rel = pathlib.Path('temp/dir/sub_dir/../../file.txt')
In [40]:
print(p_file_rel)
In [41]:
print(p_file.samefile(p_file_rel))
In [42]:
print(p_file.parents[0])
In [43]:
print(p_file.parents[1])
In [44]:
print(p_file_rel.parents[0])
In [45]:
print(p_file_rel.parents[1])
In [46]:
print(p_file_rel.parents[2])
In [47]:
print(p_file_rel.parents[3])
In [48]:
print(p_file_rel.resolve())
In [49]:
print(p_file_rel.resolve().relative_to(p_file_rel.cwd()))
In [50]:
print(p_file.with_name('file_new.txt'))
In [51]:
print(type(p_file.with_name('file_new.txt')))
In [52]:
print(p_dir.with_name('dir_new'))
In [53]:
print(p_dir.with_name('file_new.txt'))
In [54]:
p_file.with_name('file_new.txt').touch()
In [55]:
print(p_file.with_name('file_new.txt').exists())
In [56]:
print(p_file.with_suffix('.text'))
In [57]:
print(type(p_file.with_suffix('.text')))
In [58]:
# print(p_file.with_suffix('text'))
# ValueError: Invalid suffix 'text'
In [59]:
import shutil
shutil.rmtree('temp')