Questions tagged [pathlib]

This tag is for questions about the Python built-in library pathlib, which offers classes representing filesystem paths with semantics appropriate for different operating systems.

This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between Pure paths, which provide purely computational operations without I/O, and Concrete paths, which inherit from pure paths but also provide I/O operations.

Its strength lies in that it offers a powerful Object Oriented interface for working with paths, as opposed to the string-based interface offered by os.path.

This table offers a mapping between various os functions to their corresponding PurePath/Path equivalent.

635 questions
0
votes
1 answer

Python pathlib on Windows can't expand user

Expanding "~" using Pathlib gives the wrong path. from pathlib import Path ex = Path('~/Documents') print(ex.expanduser()) I'm expecting "C:\Users{username}\Documents", but instead get "C:\WINDOWS\system32\config\systemprofile\Documents". Is there…
Jordan Sim
  • 67
  • 9
0
votes
1 answer

How to compare paths except last part

Hi guys I want to check if my path is included in the list of paths which are allowed. My problem is I cant just di if path1 in path_list or something cause path1 includs more then just the path it also has a file in th end of the path. The list…
Newone90
  • 3
  • 2
0
votes
1 answer

Easiest way to test casefolded equality for two pathlib.Paths?

In Python 3.7 or higher I want to test two pathlib.Path objects p1 and p2 for casefolded equality. Written out I want the result of str(p1).casefold() == str(p2).casefold() Is there a built-in function or operator for this? Or an easier/shorter way…
halloleo
  • 9,216
  • 13
  • 64
  • 122
0
votes
1 answer

Python cloudpathlib download all files with suffix(csv) from AWS s3

I am using cloudpathlib to download bulk files from AWS s3. My question is how can I download only csv files using this package? from cloudpathlib import CloudPath root_dir = CloudPath('s3://dir1/dir2/*.csv') root_dir.download_to(to_path) In…
pnna
  • 113
  • 1
  • 6
0
votes
1 answer

Unit test: patching os join overwrites os join in test file

I'm writing a unit test for a function that creates a folder using os.makedirs and os.path.join. When I'm trying to mock the specific os.path.join used in the class BuilddataHelpers it also patches the os.path.join in the test file. How can I…
smbfml
  • 46
  • 4
0
votes
1 answer

Python join directory paths

I have problems with os.path.join() because it never joins complete path. Code is: get_base_dir = (os.getenv('BUILD_DIRECTORY')) base_dir_path = pathlib.Path(get_base_dir) print (base_dir_path ) # output is: F:\file\temp\ - which is correct s_dir =…
John
  • 230
  • 2
  • 12
0
votes
1 answer

Define path using string value with pathlib

I am trying to design a function that iterates over a script. The arguments of this function are first_name and second_name. Among other things, this loop should create folders and subfolders as follows: def script(first_name='', second_name=''): …
0
votes
3 answers

How do you read lines from all files in a directory?

I have two files in a directory. I'd like to read the lines from each of the files. Unfortunately when I try to do so with the following code, there is no output. from pathlib import Path p = Path('tmp') for file in p.iterdir(): …
jgg
  • 791
  • 4
  • 17
0
votes
2 answers

Open a file with a specific extension, regardless of the name

I have the following code: folder_names = [] spreadsheet_contents = [] all_data = pd.DataFrame() current_directory = Path.cwd() for folder in current_directory.iterdir(): folder_names.append(folder.name) file_n = '*.csv' …
0
votes
1 answer

Schedule Monthly Emails in Python using SQL results as attachment

I'm looking to query a database in Microsoft SQL Server Management Studio > save the query as an xlsx document > save the document on my computer > email that same document as an attachment to another user every month. I've got to the point where I…
Mystical Me
  • 137
  • 6
0
votes
2 answers

can't be able to import module while having modules on different folders in python

I'm having project structure like below code | -- core -- | | | test1.py | test2.py |---database-- | | | sample.py --main.py main.py is entry point for my application, from main.py I can…
Saranraj K
  • 412
  • 1
  • 7
  • 19
0
votes
0 answers

Different between re.match and Path.match

I have a list of paths with files name. The file's name can be random. I tried using re module with regex tested using online tools. But it return none. Using pathlib.Path.match return what I wanted. l = ["C:/dir/1.png", "C:/dir/a2.png",…
AdvMaple
  • 99
  • 1
  • 7
0
votes
2 answers

How to join base path with path starting from current directory

In the following code, dirpath starts from current directory something like ./x/y. Then I want to add fullname of current directory to it, but it results /root/./x/y/file.txt, but I want /root/x/y/file.txt def get_files(self): cur_path =…
Ahmad
  • 8,811
  • 11
  • 76
  • 141
0
votes
1 answer

Python Pathlib glob fails on long pathnames on windows - how to ignore the error?

I am trying to walk through all files on an "exFAT" formatted drive on Windows 10 from pathlib import Path def scan_drive(drive_letter): for f in Path(f"{drive_letter}:\\").glob("**/*"): # do something, code removed, print instead …
576i
  • 7,579
  • 12
  • 55
  • 92
0
votes
2 answers

Creating data frames within Pandas from 2 most recent .csv within a directory

I have a directory that is consistently having .csv files appended (1 or 2 every 30 min). My pandas script merges and cleans two of the latest .csv within the dir (two paths are currently added manually) and then saves a .csv of their differences…
DECROMAX
  • 194
  • 3
  • 13