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 str JohnnyCoffee 2 2,951 May-01-2021, 03:58 PM
Last Post: JohnnyCoffee
  Concatenate two dataframes moralear27 2 1,897 Sep-15-2020, 08:04 AM
Last Post: moralear27
  Output CSV file with filepath and file contents glittergirl 1 1,761 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,595 Jul-16-2020, 07:46 AM
Last Post: svkroy
  can only concatenate str (not "int") to str gr3yali3n 6 4,147 May-28-2020, 07:20 AM
Last Post: pyzyx3qwerty
  pytest in CircleCI filepath error alyflex 0 1,279 Apr-20-2020, 04:39 PM
Last Post: alyflex
  Concatenate two dictionaries harish 3 2,404 Oct-12-2019, 04:52 PM
Last Post: strngr12
  Reading and writing Windows filepath without treating backslash as escape character Dangthrimble 3 3,550 Dec-18-2017, 06:18 PM
Last Post: DeaD_EyE
  Get filepath to save from notepad.exe python3 Jason2024 1 2,990 Oct-03-2017, 02:30 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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