LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2013, 06:20 PM   #1
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Rep: Reputation: 40
Python: how to concatenate two strings into a filename for shutil.copy


I have been struggling for a couple of hours trying to suss this out. I have parsed directories and files to find which files I want to copy to somewhere else. I have this:

Code:
for aFile in os.listdir(thispath):
	aPath = os.path.join(thispath, aFile)
	print aPath
	dest = thispath
	vs = thispath.split(rootdir).pop()
	dPath = os.path.join(destination_root,vs)
        print destination_root + vs
        print dPath
Although aPath is fine and

destination_root + vs

prints out correctly, dPath is just vs

vs is taking the Path of the files in the directory I'm copying and stripping out the start directory I am parsing from. So why doesn't this line:

dPath = os.path.join(destination_root,value)

Give me the same as destination_root + value ?

I want that to be right so I can use:

Code:
if os.path.isfile(aPath):	  
    shutil.copy(aPath,dPath)
Or maybe in other words how do I take a valid string and turn it into something shutil.copy will recognise as a valid directory?!

Hope that makes sense! Thanks

Last edited by DiBosco; 02-10-2013 at 06:24 PM.
 
Old 02-10-2013, 07:45 PM   #2
DiBosco
Member
 
Registered: Nov 2001
Location: Manchester, UK
Distribution: Mageia
Posts: 807

Original Poster
Rep: Reputation: 40
Ach, the problem was a leading / in the filename of the destination file. Stripping that out makes it work.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate strings in bash sadosan83 Programming 7 01-13-2012 01:48 AM
showing the number of strings in the output filename udiubu Programming 3 11-08-2011 12:28 PM
[SOLVED] Python cannot concatenate 'str' and 'NoneType' objects madsovenielsen Programming 4 06-24-2010 01:44 PM
[SOLVED] How to concatenate strings in Shell kofucii Linux - Newbie 2 12-12-2009 05:07 AM
How to concatenate two strings into one string in B-shell? jimmyjiang Red Hat 5 01-08-2008 01:15 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration