Python Forum
Replacing String Variable with a new String Name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing String Variable with a new String Name
#1
Hello,

It has been a while since I have coded in C++, and I am new to python since I am currently making macros for a CFD software called STAR-CCM.

Backround
I plan on running a sim on the cluster, which means I have CAD software generating a new geometry file for the number of iterations I want, and then importing the geometry file into the CFD software. My macro is created to import the geometry file into the CFD software. The problem is, in every iteration, the geometry file is named differently, such as "geom_{#OfIteration}." So iteration 1 will have a geometry file name "geom_1," iteration 2 has the name "geom_2" and so on...

My Goal
So, my goal is to create a macro that looks for the current geometry file name and renames it to a new name since I already have a STAR .sim file that has mesh settings only for a geometry file named "geom_0." So, therefore, I want to name every geometry file name to "geom_0."

What I know the code should do
Since I have some background in C++, I know that I should create an if-then statement. For instance, I want the code to parse through the file name, and if the name contains "geom" (which it will), then I want the name to be replaced with "geom_0." I am unfamiliar with python syntax, but can someone guide me in achieving this goal? I assume it won't be that difficult since I just want to save the file name as a new name. Maybe I am wrong though.
Reply
#2
you can do this using the builtin package shutil.
Reply
#3
(Jul-28-2023, 03:30 PM)kevv11 Wrote: I assume it won't be that difficult since I just want to save the file name as a new name. Maybe I am wrong though.
It's not to difficult,we preferer that someone at least give it try with code even if a wrong approach.
Now its's just like job deception of the the task.
That said here some basic step that can be used for this.
from pathlib import Path
import os

dest = r'C:\tests'
os.chdir(dest)
for path in Path(dest).rglob('*.sim'):
    if path.is_file():
        print(path)
Output:
C:\tests\geom_999.sim C:\tests\requirements.sim
So we iterate over folder tests and only get file extension with .sim
pathlib is newer way(or it's been almost 10-yers since come out),to work with filesystem paths.
So on more step,let find files with geom in name and replace.
from pathlib import Path
import os

dest = r'C:\tests'
os.chdir(dest)
new_name = 'geom_0.sim'
for path in Path(dest).rglob('*.sim'):
    if path.is_file():
        if 'geom' in path.name:
            #print(path)
            os.rename(path.name, new_name)
So now is geom_999.sim remaned to geom_0.sim in folder tests.
os.chdir(dest) so this can be run from any path,a common mistake with this is file not found because not running is dest path.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I parse the string? anna17 4 345 Apr-10-2024, 10:26 AM
Last Post: DeaD_EyE
  remove gilberishs from a "string" kucingkembar 2 284 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Matching string from a file tester_V 5 454 Mar-05-2024, 05:46 AM
Last Post: Danishhafeez
  Python3 string slicing Luchano55 4 632 Feb-17-2024, 09:40 AM
Last Post: Pedroski55
  Retrieve word from string knob 4 504 Jan-22-2024, 06:40 PM
Last Post: Pedroski55
  Writing a Linear Search algorithm - malformed string representation Drone4four 10 972 Jan-10-2024, 08:39 AM
Last Post: gulshan212
  Virtual Env changing mysql connection string in python Fredesetes 0 386 Dec-20-2023, 04:06 PM
Last Post: Fredesetes
  Comparing Dataframe to String? RockBlok 2 419 Nov-24-2023, 04:55 PM
Last Post: RockBlok
  Sample random, unique string pairs from a list without repetitions walterwhite 1 465 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  extract substring from a string before a word !! evilcode1 3 554 Nov-08-2023, 12:18 AM
Last Post: evilcode1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020