Manipulate variables in Python [duplicate] - python

This question already has answers here:
How to get only the last part of a path in Python?
(10 answers)
Closed 5 years ago.
Is it possible to manipulate a variable, for example:
file = "/Python/work.txt"
to just list work.txt, without /Python? excluding everything on the left of the "/"?
Thank you

Of course! Simply do this:
file = "/Python/work.txt"
excluded = file.split("/")[-1]
This would return "work.txt" in the excluded variable.

Related

How can I find full path for a file? [duplicate]

This question already has answers here:
How to get an absolute file path in Python
(11 answers)
Closed 2 years ago.
Let's say I have this path
D:\something\something1\from_here_I_now\stuff\stuff2.
So, I know that \from_here_I_now\stuff\stuff2 is a permanent path, but the beginning is different, like I know that D:\something\something1\ may be different for someone else. How can I find the D:\something\something1\ knowing only \from_here_I_now\stuff\stuff2?
Try something like this:
import os
filestr = '\from_here_I_now\stuff\stuff2'
fullstr = os.path.abspath(filestr)
print(fullstr)
>>> 'D:\something\something1\from_here_I_now\stuff\stuff2'
print(fullstr[:len(filestr)])
>>> 'D:\something\something1'

looping through elements and using them as names for objects in python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
eval(): can't assign to function call
(1 answer)
Closed 2 years ago.
for N_RECORDS in ['50k','50m','100m','200m','python']:
eval("res_"+N_RECORDS) = spark.read.load("/tmp/xgb-rts/xgb_results_"+N_RECORDS+"_pos.csv")
eval("res_"+N_RECORDS+"_neg")=spark.read.load("/tmp/xgb-rts/xgb_results_"+N_RECORDS+"_neg.csv")
I just want to create objects with the names res_50k, res_50m, res_100m, res_50k_neg, etc without doing it manually.
However, I keep getting an error
SyntaxError: can't assign to function call
File "<command-1997330779535506>", line 2
eval("res_"+N_RECORDS) = spark.read.load("/tmp/xgb-rts/xgb_results_"+N_RECORDS+"_pos.csv"
How do I work around this?

Make a list with multiple possible strings from file names with regex [duplicate]

This question already has answers here:
Regex match one of two words
(2 answers)
Closed 3 years ago.
I want to make a list of several PNG in a folder based on multiple references. So in the list I want the PNG that have the string "7029113" OR "7031503" in their name. This is what I got so far, I only need to know how to do OR with regex, and probably my wildcards are wrong too I'm not sure.
render_path = "C:/BatchRender/Renaming"
os.chdir(render_path)
list_files = glob.glob("*.png")
r = re.compile(".*7029113.*" OR ".*7031503.*")
list_40 = list(filter(r.match, list_files))
This is one way of doing it.
r = re.compile('.*(7029113|7031503).*')

List of strings. New variables with those name (without classes). Python [duplicate]

This question already has answers here:
How can you dynamically create variables? [duplicate]
(8 answers)
Closed 8 years ago.
if one had a list of strings, how could they create variables (empty list objects) with those names?
pre_vars= ['list_1','list_2','list_3']
print list_1,list_2,list_3
>>> [],[],[]
I saw some examples that were similar but they were using classes. Can this be done without using classes?
Use globals():
for name in pre_vars:
globals()[name]= []

cut the right side of a '=' delimited string and storing into a list in python [duplicate]

This question already has answers here:
How to get part of string and pass it to other function in python?
(4 answers)
Closed 8 years ago.
A beginner question
My file looks like -->
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
How can I cut the IP part and store it into a list for further processing?
Please help.
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())

Categories