pathlib

Pathlib 'normalizes' UNC paths with “$”

感情迁移 提交于 2021-02-16 14:23:51
问题 On Python3.8, I'm trying to use pathlib to concatenate a string to a UNC path that's on a remote computer's C drive. It's weirdly inconsistent. For example: >>> remote = Path("\\\\remote\\", "C$\\Some\\Path") >>> remote WindowsPath('//remote//C$/Some/Path') >>> remote2 = Path(remote, "More") >>> remote2 WindowsPath('/remote/C$/Some/Path/More') Notice how the initial // is turned into / ? Put the initial path in one line though, and everything is fine: >>> remote = Path("\\\\remote\\C$\\Some\

Recommended way of closing files using pathlib module?

*爱你&永不变心* 提交于 2021-02-08 13:32:07
问题 Historically I have always used the following for reading files in python : with open("file", "r") as f: for line in f: # do thing to line Is this still the recommend approach? Are there any drawbacks to using the following: from pathlib import Path path = Path("file") for line in path.open(): # do thing to line Most of the references I found are using the with keyword for opening files for the convenience of not having to explicitly close the file. Is this applicable for the iterator

How to prevent pathlib’s Path.glob from returning files when the glob ends in a slash?

和自甴很熟 提交于 2021-02-05 06:02:10
问题 The the new Path.glob from pathlib seems to behave differently from the old glob.glob when the glob pattern ends in a slash it seems. In [1]: from pathlib import Path In [2]: from glob import glob In [3]: glob('webroot/*/') Out[3]: ['webroot/2017-06-07/'] In [4]: list(Path().glob('webroot/*/')) Out[4]: [PosixPath('webroot/.keep'), PosixPath('webroot/2017-06-07'), PosixPath('webroot/matches.2017-06-07.json')] Is that by design, some compatibility issue I haven’t encountered? And is there a way

Why does pathlib.Path(“C:”) resolve to the working directory on Windows?

被刻印的时光 ゝ 提交于 2021-01-27 18:22:51
问题 Using Python 3.6 on Windows 7 x64, the path "C:" seems identical to an empty path for Path.resolve() : 'Empty' paths are 'current working directory' cwd() : >>> from pathlib import Path >>> Path().resolve() WindowsPath('C:/Users/me') >>> Path(r"").resolve() WindowsPath('C:/Users/me') >>> Path.cwd().resolve() WindowsPath('C:/Users/me') A single letter is interpreted as a folder name: >>> Path(r"C").resolve() WindowsPath('C:/Users/me/C') A full blown drive-letter + colon + backslash points to

Why does pathlib.Path(“C:”) resolve to the working directory on Windows?

孤人 提交于 2021-01-27 17:59:04
问题 Using Python 3.6 on Windows 7 x64, the path "C:" seems identical to an empty path for Path.resolve() : 'Empty' paths are 'current working directory' cwd() : >>> from pathlib import Path >>> Path().resolve() WindowsPath('C:/Users/me') >>> Path(r"").resolve() WindowsPath('C:/Users/me') >>> Path.cwd().resolve() WindowsPath('C:/Users/me') A single letter is interpreted as a folder name: >>> Path(r"C").resolve() WindowsPath('C:/Users/me/C') A full blown drive-letter + colon + backslash points to

How to check folder / file permissions with Pathlib

房东的猫 提交于 2021-01-02 07:57:50
问题 Is there a Pathlib equivalent of os.access() ? Without Pathlib the code would look like this: import os os.access('my_folder', os.R_OK) # check if script has read access to folder However, in my code I'm dealing with Pathlib paths, so I would need to do this (this is just an example): # Python 3.5+ from pathlib import Path import os # get path ~/home/github if on Linux my_folder_pathlib = Path.home() / "github" os.access(str(my_folder_pathlib), os.R_OK) The casting to str() is kinda ugly. I

What's the best way to add a trailing slash to a pathlib directory?

ε祈祈猫儿з 提交于 2020-12-01 09:51:32
问题 I have a directory I'd like to print out with a trailing slash: my_path = pathlib.Path('abc/def') Is there a nicer way of doing this than os.path.join(str(my_path), '') ? 回答1: No, you didn't miss anything. By design, pathlib strips trailing slashes, and provides no way to display paths with trailing slashes. This has annoyed several people, as mentioned in the bug tracker: pathlib strips trailing slash. A compact way to add slashes in Python 3.6 is to use an f-string, eg f'{some_path}/' or f'

Best way to handle path with pandas

家住魔仙堡 提交于 2020-08-24 06:49:20
问题 When I have a pd.DataFrame with paths, I end up doing a lot of .map(lambda path: Path(path).{method_name} , or apply(axis=1) e.g: ( pd.DataFrame({'base_dir': ['dir_A', 'dir_B'], 'file_name': ['file_0', 'file_1']}) .assign(full_path=lambda df: df.apply(lambda row: Path(row.base_dir) / row.file_name, axis=1)) ) base_dir file_name full_path 0 dir_A file_0 dir_A/file_0 1 dir_B file_1 dir_B/file_1 It seems odd to me especially because pathlib does implement / so that something like df.base_dir /

I can't load my model because I can't put a PosixPath

守給你的承諾、 提交于 2020-07-22 05:46:05
问题 I'm setting up a script and I need to use some functions from fast-ai package. The fact is that I'm on Windows and when I define my paths, the function from fast-ai named load_learner can't load the model. I've tried to change the function into the package as: state = pickle.load(open(str(path) + '/' + str(fname), 'rb')) instead of: state = pickle.load(open(path/fname, 'rb')) but I obtain this error: File "lib\site-packages\fastai\basic_train.py", line 462, in load_learner state = pickle.load