Using variable and strings to create name with posixpath - python

I want to create a path with python pathlib using a variable.
This is of course incorrect due to mixing of string and posixpath:
from pathlib import Path
stringvariable='aname'
Path(Path.cwd() / 'firstpartofname_' +stringvariable+ '.csv')
I know I could do it with os, or in two lines like this:
filename='firstpartofname_' + stringvariable + '.csv'
Path(Path.cwd() / filename)
but I want to learn how to use it directly with Path.
Thanks

You just need to add parentheses to force the + to happen before the /.
new = Path.cwd() / ('firstpartofname_' + stringvariable + '.csv')

Related

What is the most pythonic way to join or construct paths?

Is it using the os.path.join() method, or concatenating strings? Examples:
fullpath1 = os.path.join(dir, subdir)
fullpath2 = os.path.join(dir, "subdir")
fullpath3 = os.path.join("dir", subdir)
fullpath4 = os.path.join(os.path.join(dir, subdir1), subdir2)
etc
or
fullpath1 = dir + "\\" + subdir
fullpath2 = dir + "\\" + "subdir"
fullpath3 = "dir" + "\\" + subdir
fullpath4 = dir + "\\" + subdir1 + \\" + subdir2"
etc
Edit with some more info.
This is a disagreement between a colleague and I. He insists the second method is "purer", while I insist using the built in functions are actually "purer" as it would make it more pythonic, and of course it makes the path handling OS-independent.
We tried searching to see if this question had been answered before, either here in SO or elsewhere, but found nothing
In my opinion (I know, no one asked) it is indeed using Path from pathlib
import pathlib
folder = pathlib.Path('path/to/folder')
subfolder = folder / 'subfolder'
file = subfolder / 'file1.txt'
Please read into pathlib for more useful functions, one I often use is resolve and folder.exists() to check if a folder exist or subfolder.mkdir(parents=True, exist_ok=True) to create a new folder including its parents. Those are random examples, the module can do a lot more.
See https://docs.python.org/3/library/pathlib.html
You can either use the first method using os.join().
A second option is to use the Pathlib module as #DeepSpace suggested.
But the other option is way worse and harder to read so you shouldn't use it.

Python Data frame to CSV - Converting to Windows Filepath

The following code works for me on the MAC OS but I'm having trouble converting it to a windows file path. If I just make the forward slashes into back slashes the file turns to color red as if it is a literal.
dict=pd.DataFrame(elements_count, index=[0,])
dict.to_csv(path+'/Word_freq/'file+'.csv')
Thank you in advance for any help.
You can use os.path.join
import os
fullPath = os.path.join(path,'Word_freq', file + '.csv')
In fact, I'd always prefer os.path.join over string concatenation, cause it will join the path based on the platform
But if you still want to use string concatenation, then you can join the path, directory, subdirectory, or file name using separting them by by os.sep
Like this:
fullPath = path + os.sep + 'Word_freq' + os.sep + file + '.csv'

Best way to join two paths?

I'm trying to understand the best way to join two paths in Python. I'm able to get my expected result by using string concatenation, but I understand that is not the preferred way of working with paths. I'm trying to preserve the folder structure of a file, but move it to a new defined output directory.
For example -
import os
orig_file = r"F:\Media\Music\test_doc.txt"
output_dir = r"D:\output_dir"
## preferred method, but unexpected result
new_file = os.path.join(output_dir, os.path.splitdrive(orig_file)[1])
print(new_file)
## new file = D:\Media\Music\test_doc.txt
## What I want
new_file = output_dir + os.path.splitdrive(orig_file)[1]
print(new_file)
## new file = D:\output_dir\Media\Music\test_doc.txt
As you can see, when I use os.path.join() it seems to discard the "output_dir" folder on the D: drive.
In Python 3.4+, the best way to do it is via the more modern and object-oriented pathlib module.
from pathlib import Path
orig_file = Path(r"F:\Media\Music\test_doc.txt")
output_dir = Path(r"D:\output_dir")
new_file = output_dir.joinpath(*orig_file.parts[1:])
print(f'{new_file=}') # -> new_file=WindowsPath('D:/output_dir/Media/Music/test_doc.txt')

Add character in a link

I want to add an character to a link.
The link is C:\Users\user\Documents\test.csv I want to add C:\Users\user\Documents\test_new.csv.
So you can see I added the _new to the filename.
Should I extract the name with Path(path).name) and then with Regex? What is the best option for do that?
As you said you want to "add" _new and not rename here is your solution and it is tiny just 2 lines of code apart from the varaible and the result, this is solution might be complex because i have compressed the code to take less memory and do the work fast, you could also change the keyword and the extension from the OUTPUT FUNCTION arguments
PATH = "C:\\User\\Folder\\file.csv"
def new_name(path, ext="csv", keyword="_new"):
print('\\'.join(path.split("\\")[:-1])+"\\"+path.split("\\")[-1].split(".")[0] + keyword + "." + ext)
new_name(PATH)
Here's a solution using the os module:
path = r"C:\User\Folder\file.csv"
root, ext = os.path.splitext(path)
new_path = f'{root}_new{ext}'
And here's one using pathlib:
path = pathlib.Path(r"C:\User\Folder\file.csv")
new_path = str(path.with_stem(path.stem + '_new'))

How can I get the pathname of the folder two directories upstream of a file?

Using glob2 and os I would like the directory '/a/b/' given the file path '/a/b/c/xyz.txt'
I have been able to (recursively) move forward through directories using /* and /** in glob2, but not backwards through parent directories. I don't want to use regular expressions or split. Is there a simple way to do this using glob and/or os?
Why glob?
dir_path = file_path.split('/')
what_i_want = '/' + dir_path[10] + '/' + dir_path[1] + '/'
You can also do this by finding the index of the 3rd slash, using the return of each call as the "start" argument to the next.
third_slash = file_path.index('/', file_path.index('/', file_path.index('/')+1) +1)
what_i_want = file_path[:third_slash+1]

Categories

Resources