Adding a simple value to a string - python

If I have a string lets say ohh
path2 = '"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio'
And I want to add a " at the end of the string how do I do that? Right now I have it like this.
path2 = '"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio'
w = '"'
final = os.path.join(path2, w)
print final
However when it prints it out, this is what is returned:
"C:\Users\bgbesase\Documents\Brent\Code\Visual Studio\"
I don't need the \ I only want the "
Thanks for any help in advance.

How about?
path2 = '"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio' + '"'
Or, as you had it
final = path2 + w
It's also worth mentioning that you can use raw strings (r'stuff') to avoid having to escape backslashes. Ex.
path2 = r'"C:\Users\bgbesase\Documents\Brent\Code\Visual Studio'

just do:
path2 = '"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio' + '"'

I think the path2+w is the simplest answer here but you can also use string formatting to make it more readable:
>>> path2 = '"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio'
>>> '{}"'.format(path2)
'"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio"'
If path2 was long than it's much easier to use string formatting than adding a + at the end of the string.
>>> path2 = '"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio\\Documents\\Brent\\Code\\Visual Studio\\Documents\\Brent\\Code\\Visual Studio'
>>> w = '"'
>>> "{}{}".format(path2,w)
'"C:\\Users\\bgbesase\\Documents\\Brent\\Code\\Visual Studio\\Documents\\Brent\\Code\\Visual Studio\\Documents\\Brent\\Code\\Visual Studio"'

From Python documentation Common pathname manipulations section:
The return value is the concatenation of path1, and optionally path2,
etc., with exactly one directory separator (os.sep) following each
non-empty part except the last.
In this case, os.path.join() treats your string '"' as path part and adds the separator. Since you are not joining two parts of path you need to use string concatenation or string formatting.
The simplest would be just to add two strings:
final = path2 + '"'
You can actually modify path2 using += operator:
path2 += '"'

Related

Combining values with text in python function

I have a python script that scrapes a webpage and downloads the mp3s found on it.
I am trying to name the files using elements that I have successfully captured in a separate function.
I am having trouble naming the downloaded files, this is what I have so far:
def make_safe_filename(text: str) -> str:
"""convert forbidden chars to underscores"""
return ''.join(c if (c.isalnum() or c.isspace()) else '_' for c in text).strip('_ ')
filename = make_safe_filename(a['artist'] + a['title'] + a['date'] + a['article_url'])
I am trying to save the file name as "Artist - Title - Date - Article_url" however I am struggling to do this. At the moment the variables are all mashed together without spaces, eg. ArtistTitleDateArticle_url.mp3
I've tried
filename = make_safe_filename(a['artist'] + "-" + a['title'] + "-" + a['date'] + "-" +
a['article_url'])
but this throws up errors.
Can anyone shed some light on where I am going wrong? I know it's something to do with combining variables but I am stuck. Thanks in advance.
I am guessing your a is a dictionary? Maybe you could clarify this in your question? Also what do you typically have in a['article_url']? Could you also post the traceback?
This is my attempt (note: no changes to the function):
def make_safe_filename(text: str) -> str:
"""convert forbidden chars to underscores"""
return ''.join(c if (c.isalnum() or c.isspace()) else '_' for c in text).strip('_ ')
a = {
'artist': 'Metallica',
'title': 'Nothing Else Matters',
'date': '1991',
'article_url': 'unknown',
}
filename = make_safe_filename(a['artist'] + '-' + a['title'] + '-' + a['date'] + '-' + a['article_url'])
print(filename)
Which produced the following output:
Metallica_Nothing Else Matters_1991_unknown
You code should actually work, but if you add the - before passing the joined string to the function, it will just replace those with _ as well. Instead, you could pass the individual fields and then join those in the function, after replacing the "illegal" characters for each field individually. Also, you could regular expressions and re.sub for the actual replacing:
import re
def safe_filename(*fields):
return " - ".join(re.sub("[^\w\s]", "_", s) for s in fields)
>>> safe_filename("Art!st", "S()ng", "ยง$%")
'Art_st - S__ng - ___'
Of course, if your a is a dictionary and you always want the same fields from that dict (artist, title, etc.) you could also just pass the dict itself and extract the fields within the function.
I had a similar problem recently, the best solution is probably to use regex, but I'm too lazy to learn that, so I wrote a replaceAll function:
def replaceAll(string, characters, replacement):
s = string
for i in characters:
s = s.replace(i, replacement)
return s
and then I used it to make a usable filename:
fName = replaceAll(name, '*<>?|"/\\.,\':', "")
in your case it would be:
filename = replaceAll(a['artist'] + a['title'] + a['date'] + a['article_url'], '*<>?|"/\\.,\':', "-")

Single liner to add text to filename before extension with f-strings

I want to add some text to a file name before the extension. Example: name.ext >>> name_su.ext.
I can do it with traditional python string formatting:
filename = 'name.ext'
suffix = '_su'
print("{0}{2}{1}".format(*os.path.splitext(filename) + (suffix, )))
# This will print: name_su.ext
I wonder if I can achieve the same with f-string notation, in a single line, without calling os.path.splitext(filename) twice.
Calling os.path.splitext() twice might be avoidable, but it's fast and readable:
print(f'{os.path.splitext(filename)[0] + suffix + os.path.splitext(filename)[1]}')

Strip and split at same time in python

I'm trying to split and strip one string at same time.
I have a file D:\printLogs\newPrintLogs\4.txt and I want to split it that I get only 4.txt and than to strip the .txt and add in string + ".zpr" to get "4.zpr".
This is the code that I tryed to use:
name = str(logfile)
print ("File name: " + name.split('\\')[-1] + name.strip( '.txt' ))
But I get this output:
File name: 4.txtD:\printLogs\newPrintLogs\4
Don't use stripping and splitting.
First of all, stripping removes all characters from a set, you are removing all 't', 'x' and '.' characters from the start and end of your string, regardless of order:
>>> 'tttx.foox'.strip('.txt')
'foo'
>>> 'tttx.foox'.strip('xt.')
'foo'
Secondly, Python offers you the os.path module for handling paths in a cross-platform and consistent manner:
basename = os.path.basename(logfile)
if basename.endswith('.txt'):
basename = os.path.splitext(basename)[0]
You can drop the str.endswith() test if you just want to remove any extension:
basename = os.path.splitext(os.path.basename(logfile))[0]
Demo:
>>> import os.path
>>> logfile = r'D:\printLogs\newPrintLogs\4.txt'
>>> os.path.splitext(os.path.basename(logfile))[0]
'4'
You're adding too much there. This is all you need:
print ("File name: " + name.split('\\')[-1].strip( '.txt' ))
Better yet, use the os module:
>>> import os
>>> os.path.splitext(os.path.basename(r'D:\printLogs\newPrintLogs\4.txt'))[0]
'4'
Or, split up among several steps, with occasional feedback:
>>> import os
>>> name = r'D:\printLogs\newPrintLogs\4.txt'
>>> basename = os.path.basename(name)
>>> basename
'4.txt'
>>> splitname = os.path.splitext(basename)
>>> splitname
('4', '.txt')
>>> splitname[0]
'4'
Thank you all for your solutions it helped me but at first I didn't explained question right.
I founded solution for my problem
name = str(logfile)
print ("Part name: " + name.split('\\')[-1].replace('.txt','.zpr'))
For python 3.4 or later:
import pathlib
name = r"D:\printLogs\newPrintLogs\4.txt"
stem = pathlib.Path(name).stem
print(stem) # prints 4
You can split and rstrip:
print(s.rsplit("\\",1)[1].rstrip(".txt"))
But it may be safer to split on the .:
print(s.rsplit("\\",1)[1].rsplit(".",1)[0])
If you rstrip or strip you could end up removing more than just the .txt .

strip() function doesn't remove trailing numbers

I try the following code but fails to remove the trailing digits using python 3.4.3
file_name = "48athens22.jpg"
result = file_name.strip("0123456789")
print (result)
Output:
athens22.jpg
What has gone wrong?
strip() only strips from the end of a string; the 22 is not at the end of the string.
Here's how to do what you want:
import os
def strip_filename(filename):
root, ext = os.path.splitext(filename)
root = root.strip('0123456789')
return root + ext
print(strip_filename('48athens22.jpg')) # athens.jpg
strip only removes from the beginning and end of the string. Try re.sub instead, if you want to remove any occurrences of a substring or a pattern.
E.g.
re.sub('[0-9]', '', file_name)
Those numbers aren't trailing. They come before the '.jpg'.
file_name = "48athens22.jpg"
name, *extension = file_name.rpartition('.')
result = name.strip("0123456789") + ''.join(extension)
print (result)
Works for me:
file_name = "48athens22.jpg1234"
result = file_name.strip("0123456789")
print(result)
Gives:
athens22.jpg
If you want to remove all digits, try:
import re
file_name = "48athens22.jpg1234"
result = re.sub(r'\d+', "", file_name)
print(result)
Gives:
athens.jpg
If you only want to remove digits before the ".", try:
result = re.sub(r'\d+\.', ".", file_name)
There are no trailing numbers , the last character in your string in 'g' , 22 is actually in the middle , if you do not want to consider the extension when striping , you will have to first split the file_name based on '.' And then strip the first part and then rejoin them.
Code -
filenames = file_name.split('.')
result = filenames[0].strip('0123456789') + '.' + '.'.join(filenames[1:])
print(result)

concatenating string in python

I am converting a command line to a python string. The command line is:
../src/clus -INFILE=../input/tua40.sq -OUTPUT=OUT
The python statement is:
c_dir = '~/prj/clus/'
c_bin = c_dir + 'src/clus'
c_data = c_dir + 'input/tua40.sq'
c = LiveProcess()
c.executable = c_bin
c.cwd = c_dir
c.cmd = [c.executable] + ['-INFILE=', 'c_data, '-OUTPUT=OUT']
Problem is the c.cmd at the end looks like
~/prj/clus/src/clus -INFILE= ~/prj/clus/input/tua40.sq ...
Not that there is a 'space' after '=' which causes the program to report an error.
How can I concatenate '=' to the path?
LiveProcess is expecting an argv-style list of arguments. Where you want to make one argument, you need to provide one string. So use concatenation to make the string:
c.cmd = [c.executable] + ['-INFILE='+c_data, '-OUTPUT=OUT']
Also, no need for the list addition:
c.cmd = [c.executable, '-INFILE='+c_data, '-OUTPUT=OUT']
Why don't you just concatenate string like this:
a = 'A'+'B'
then
a == 'AB'
that is in your example
['-INFILE=' + c_data, '-OUTPUT=OUT']
Given that it looks like you're concatenating paths, you should be using os.path.join, not regular string concat.
Try this:
c.cmd = [c.executable] + ['-INFILE='+c_data, '-OUTPUT=OUT']

Categories