Python Forum
How to concatenate filepath with variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to concatenate filepath with variable?
#1
Hi all,

I'm trying to write a program that will replace a couple lines of a new program (placed in a set filepath) and replace a couple filepath lines to reflect my computer rather than the one who wrote it.

He uses spaces in his filename, which seems to causes issues for me so I'm stripping out the spaces.

I'm having particular trouble with Line 18, though:

#This program will take John's new code files from Input folder and save code with my filepath in Output folder

import glob, os, shutil

feed_dir = r"C:\Users\Mark\Desktop\john_filepath_conversion\Input"

os.chdir(feed_dir)

for file in glob.glob("*.*"):
    barfile = open(file, "r")   #file is the string containing each filename returned by the glob function
    print(f'Orig name is {barfile.name}.')
    new_name = barfile.name.replace(' ','')
    print(f'New name is {new_name}.')
    barfile.close()

#os.rename(barfile.name,new_name)

shutil.move(barfile.name,r"C:\Users\Mark\Desktop\john_filepath_conversion\Converted\"+new_name")
#is there a way to incorporate the value of new_name into the filepath?
Thanks for any help!
Reply
#2
Can't do this: shutil.move(barfile.name,r"C:\Users\Mark\Desktop\john_filepath_conversion\Converted\"+new_name")

instead use something like:
newfilename = f"C:\Users\Mark\Desktop\john_filepath_conversion\Converted\{new_name}"
shutil.move(barfile.name,newfilename)
Edit: Jan31 - 10:21 EST missing letter fixed
BashBedlam likes this post
Reply
#3
(Jan-31-2022, 06:04 PM)Larz60+ Wrote: Can't do this: shutil.move(barfile.name,r"C:\Users\Mark\Desktop\john_filepath_conversion\Converted\"+new_name")

instead use something like:
ewfilename = f"C:\Users\Mark\Desktop\john_filepath_conversion\Converted\{new_name}"
shutil.move(barfile.name,newfilename)

Very nice!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Concatenate array for 3D plotting armanditod 1 1,292 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Concatenate str JohnnyCoffee 2 3,676 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  Concatenate two dataframes moralear27 2 2,541 Sep-15-2020, 08:04 AM
Last Post: moralear27
  concatenate a request to the endpoint of OSM-API?! - how to? apollo 0 1,641 Aug-18-2020, 03:21 PM
Last Post: apollo
  Output CSV file with filepath and file contents glittergirl 1 2,339 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  Trying to pass an exe filepath to tkinter Chatbot & expecting the chatbot to run it svkroy 0 1,976 Jul-16-2020, 07:46 AM
Last Post: svkroy
  can only concatenate str (not "int") to str gr3yali3n 6 6,111 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty
  pytest in CircleCI filepath error alyflex 0 1,657 Apr-20-2020, 04:39 PM
Last Post: alyflex
  How to concatenate multiple dataframes rajeshE 1 2,578 Mar-02-2020, 06:37 AM
Last Post: scidam
  Concatenate two dictionaries harish 3 3,164 Oct-12-2019, 04:52 PM
Last Post: strngr12

Forum Jump:

User Panel Messages

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