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
  Spaces in string Mallard 9 390 Dec-19-2025, 01:04 PM
Last Post: Mallard
  Convert any Python expression to a string voidtrance 2 1,077 Jun-23-2025, 07:06 AM
Last Post: DeaD_EyE
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,460 May-28-2025, 07:56 AM
Last Post: Winfried
  Return a string or byte object from Enum class? Calab 5 1,843 May-14-2025, 05:21 PM
Last Post: snippsat
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,609 May-01-2025, 03:43 PM
Last Post: Winfried
  Get the string after a specific char JanJan 5 1,707 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  TypeError: string indices must be integers deneme2 2 1,365 Feb-14-2025, 12:23 AM
Last Post: deneme2
  How do I parse the string? anna17 8 3,229 Feb-13-2025, 07:08 AM
Last Post: michaeljordan
  question about changing the string value of a list element jacksfrustration 4 3,073 Feb-08-2025, 07:43 AM
Last Post: jacksfrustration
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 2,839 Dec-19-2024, 11:57 AM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.