Copying files from sub folders under source directories into subfolders with same names as source di

S

satishmlwizpro

Hi,

Consider
/src/alias/a.c
/src/alias/b.c
/src/xml/p.xml
/src/xml/c.xml
/src/h.c
as source directory
and
/dest/alias
/dest/xml
/dest
as destination directory. These are given in a csv file like
/src/alias/a.c, /dest/alias
/src/alias/b.c, /dest/alias
/src/xml/p.xml, /dest/xml
/src/xml/c.xml, /dest/xml
/src/h.c, /dest
Python code should read csv file. Copy files from source to destination(files in /src/alias should be copied to /dest/alias and not to /dest/xml i.e.,directory names should be the same). If /dest contains same files as /src,then they code should give us a warning and such files shouldn't be copied.. Other files should only be copied.

Could you kindly help?
 
C

Chris Angelico

Could you kindly help?

Sure. Either start writing code and then post when you have problems,
or investigate some shell commands (xcopy in Windows, cp in Linux,
maybe scp) that can probably do the whole job.

Or pay someone to do the job for you.

ChrisA
 
S

Satish ML

Consider xls file contains source and destination directory paths.
import xlrd, sys, subprocess
file_location = "C:\Users\User1\Desktop\input.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(0, 0)
for row in range(sheet.nrows):

values = []

values.append(sheet.cell_value(row, 1))


destination = []
destination.append(sheet.cell_value(row, 2))


for s in values:

for d in destination:
What next after this?
shutil.copy(src, dest) doesn't work because it overwrites dest files.
 
S

Satish ML

Hi ChrisAngelico,

Consider that source and destination directories are given in a .xls(excel)file.

This is the code

import xlrd, sys, subprocess
file_location = "C:\Users\salingeg\Desktop\input.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(0, 0)
for row in range(sheet.nrows):

values = []

values.append(sheet.cell_value(row, 1))


destination = []
destination.append(sheet.cell_value(row, 2))


for s in values:

for d in destination:

If I am using cp or xcopy command, it will copy all files from s to d.
shutil.copy(s, d) can't be used here because it overwrites files in d. Kindly help.
 
R

Rustom Mody

Could you kindly help? Sure. Either start writing code and then post when you have problems, or investigate some shell commands (xcopy in Windows, cp in Linux, maybe scp) that can probably do the whole job. Or pay someone to do the job for you. ChrisA
Hi ChrisAngelico,
Consider that source and destination directories are given in a .xls(excel) file.
This is the code
import xlrd, sys, subprocess
file_location = "C:\Users\salingeg\Desktop\input.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(0, 0)
for row in range(sheet.nrows):
values = []
values.append(sheet.cell_value(row, 1))
destination = []
destination.append(sheet.cell_value(row, 2))
for s in values:
for d in destination:
If I am using cp or xcopy command, it will copy all files from s to d.
shutil.copy(s, d) can't be used here because it overwrites files in d. Kindly help.

have u tried using
https://docs.python.org/2/library/os.path.html#os.path.exists
?
 
S

Satish ML

I have tried it. But how does it help?

We won't be able to make out whether source file is present in destination directory.
 
S

Satish ML

If we can do that, like

if (source file exists in destination directory)
print "exists"
else
shutil.copy(s, d)
 
S

Satish ML

If we can do that, like

if (source file exists in destination directory)
print "exists"
continue
else
shutil.copy(s, d)
 
S

Satish ML

Here we don't have the option of manually giving the file path. It has to be read from .xls file (i.e. from the two lists in code)
 
J

Jagadeesh Malakannavar

Hi Satish,

Can you please send python part in plain text format? Python code here is
difficult to read.

Thanks

On Tue, 20 May 2014, Satish ML wrote:

| On Tuesday, May 20, 2014 5:54:47 PM UTC+5:30, Satish ML wrote:
| > On Tuesday, May 20, 2014 5:51:19 PM UTC+5:30, Satish ML wrote: > On Tuesday, May 20, 2014 11:27:01 AM UTC+5:30, Rustom Mody wrote: > On Monday, May 19, 2014 2:32:36 PM UTC+5:30, Satish ML wrote: > On Monday, May 19, 2014 12:31:05 PM UTC+5:30, Chris Angelico wrote: > > On Mon, May 19, 2014 at 4:53 PM, wrote: > Could you kindly help? Sure. Either start writing code and then post when you have problems, or investigate some shell commands (xcopy in Windows, cp in Linux, maybe scp) that can probably do the whole job. Or pay someone to do the job for you. ChrisA > Hi ChrisAngelico, > Consider that source and destination directories are given in a .xls(excel) file. > This is the code > import xlrd, sys, subprocess > file_location = "C:\Users\salingeg\Desktop\input.xls" > workbook = xlrd.open_workbook(file_location) > sheet = workbook.sheet_by_index(0) > sheet.cell_value(0, 0) > for row in range(sheet.nrows): > values = [] > values.append(sheet.cell_value(row, 1)) > destination = [] > dest
| ination.append(sheet.cell_value(row, 2)) > for s in values: > for d in destination: > If I am using cp or xcopy command, it will copy all files from s to d. > shutil.copy(s, d) can't be used here because it overwrites files in d. Kindly help. have u tried using https://docs.python.org/2/library/os.path.html#os.path.exists ? I have tried it. But how does it help? We won't be able to make out whether source file is present in destination directory. If we can do that, like if (source file exists in destination directory) print "exists" continue else shutil.copy(s, d)
|
| Here we don't have the option of manually giving the file path. It has tobe read from .xls file (i.e. from the two lists in code)
|
 
S

Satish ML

Hi,

Here is the code.


xls file looks as follows:
a.c C:\Desktop\salingeg\src\code\a.c C:\Desktop\salingeg\dest\code
hello.txt C:\Desktop\salingeg\src\txt\hello.txt C:\Desktop\salingeg\dest\txt
integration.doc C:\Desktop\salingeg\src\doc\integration.doc C:\Desktop\salingeg\dest\doc
UG.doc C:\Desktop\salingeg\src\doc\UG.doc C:\Desktop\salingeg\dest\doc
Applications.xml C:\Desktop\salingeg\src\xml\Applications.xml C:\Desktop\salingeg\dest\xml
Platforms.xml C:\Desktop\salingeg\src\xml\Platforms.xml C:\Desktop\salingeg\dest\xml
avc.alias C:\Desktop\salingeg\src\cnx\alias\avc.alias C:\Desktop\salingeg\dest\cnx\alias
cats.alias C:\Desktop\salingeg\src\cnx\alias\cats.alias C:\Desktop\salingeg\dest\cnx\alias
avc.init C:\Desktop\salingeg\src\cnx\init\avc.init C:\Desktop\salingeg\dest\cnx\init
cats.init C:\Desktop\salingeg\src\cnx\init\cats.init C:\Desktop\salingeg\dest\cnx\init


PYTHON SCRIPT:

import xlrd, sys, os, shutil

file_location = "C:\Users\salingeg\Desktop\input.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(0, 0)
for row in range(sheet.nrows):
source = []
source.append(sheet.cell_value(row, 1))
destination = []
destination.append(sheet.cell_value(row, 2))
files = []
files.append(sheet.cell_value(row, 0))
for f in files:
for s in source:
for d in destination:
print f
print s
print d
if (os.path.exists("d\\f")):
print ('File exists')
else:
shutil.copy(s, d)

I am getting the following error:

a.c
C:\Desktop\salingeg\src\code\a.c
C:\Desktop\salingeg\dest\code
Traceback (most recent call last):
File "C:\Users\salingeg\Desktop\excel_1.py", line 24, in <module>
shutil.copy(s, d)
File "C:\Program Files (x86)\python26\lib\shutil.py", line 84, in copy
copyfile(src, dst)
File "C:\Program Files (x86)\python26\lib\shutil.py", line 50, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: u'C:\\Desktop\\salingeg\\src\\code\\a.c'
 
P

Peter Otten

Satish ML wrote:

[Regarding subject: let's see if we can trigger a buffer overflow somewhere
;)]
> Hi Satish, > > Can you please send python part in plain text
format? Python code here is > > difficult to read. It would be helpful to
read
https://wiki.python.org/moin/GoogleGroupsPython#Posting_from_Google_Groups
Note particularly the 2 standard expectations: - Dont top post - Dont use
excessively long (> 70 chars) lines

Hi,

Here is the code.


xls file looks as follows:
a.c C:\Desktop\salingeg\src\code\a.c C:\Desktop\salingeg\dest\code
hello.txt C:\Desktop\salingeg\src\txt\hello.txt
C:\Desktop\salingeg\dest\txt
integration.doc C:\Desktop\salingeg\src\doc\integration.doc
C:\Desktop\salingeg\dest\doc
UG.doc C:\Desktop\salingeg\src\doc\UG.doc C:\Desktop\salingeg\dest\doc
Applications.xml C:\Desktop\salingeg\src\xml\Applications.xml
C:\Desktop\salingeg\dest\xml
Platforms.xml C:\Desktop\salingeg\src\xml\Platforms.xml
C:\Desktop\salingeg\dest\xml
avc.alias C:\Desktop\salingeg\src\cnx\alias\avc.alias
C:\Desktop\salingeg\dest\cnx\alias
cats.alias C:\Desktop\salingeg\src\cnx\alias\cats.alias
C:\Desktop\salingeg\dest\cnx\alias
avc.init C:\Desktop\salingeg\src\cnx\init\avc.init
C:\Desktop\salingeg\dest\cnx\init
cats.init C:\Desktop\salingeg\src\cnx\init\cats.init
C:\Desktop\salingeg\dest\cnx\init


PYTHON SCRIPT:

import xlrd, sys, os, shutil

file_location = "C:\Users\salingeg\Desktop\input.xls"
workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
sheet.cell_value(0, 0)
for row in range(sheet.nrows):
source = []
source.append(sheet.cell_value(row, 1))
destination = []
destination.append(sheet.cell_value(row, 2))
files = []
files.append(sheet.cell_value(row, 0))
for f in files:
for s in source:
for d in destination:
print f
print s
print d
if (os.path.exists("d\\f")):

The following line will either always be executed if you have a subdirectory
"d" in your current working directory and that directory contains a file
called "f" (unlikely) or never if "d\\f" doesn't exist (likely).

Have a look at os.path.join() for the right way to join a directory with a
filename into a path. Use the interactive interpreter to make sure you get
the desired result and understand how it works before you fix your script.
print ('File exists')
else:
shutil.copy(s, d)

I am getting the following error:

a.c
C:\Desktop\salingeg\src\code\a.c
C:\Desktop\salingeg\dest\code
Traceback (most recent call last):
File "C:\Users\salingeg\Desktop\excel_1.py", line 24, in <module>
shutil.copy(s, d)
File "C:\Program Files (x86)\python26\lib\shutil.py", line 84, in copy
copyfile(src, dst)
File "C:\Program Files (x86)\python26\lib\shutil.py", line 50, in
copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory:
u'C:\\Desktop\\salingeg\\src\\code\\a.c'

According to the error message the file you are trying to copy doesn't
exist. Have a look into the C:\Desktop\salngeg\src\code folder, and check
whether a file called a.c is there. If not you have three options

- add the file
- remove the line from the excel file
- modify the code to check if the *source* file exists
 
S

Satish ML

Satish ML wrote: [Regarding subject: let's see if we can trigger a bufferoverflow somewhere ;)] > On Wednesday, May 21, 2014 6:59:40 AM UTC+5:30, Rustom Mody wrote: >> On Tuesday, May 20, 2014 9:35:10 PM UTC+5:30, Jagadeesh N. Malakannavar >> wrote: > Hi Satish, > > Can you please send python part in plain text >> format? Python code here is > > difficult to read. It would be helpful to >> read >> https://wiki.python.org/moin/GoogleGroupsPython#Posting_from_Google_Groups >> Note particularly the 2 standard expectations: - Dont top post - Dont use >> excessively long (> 70 chars) lines > > Hi, > > Here is the code. > > > xls file looks as follows: > a.c C:\Desktop\salingeg\src\code\a.c C:\Desktop\salingeg\dest\code > hello.txt C:\Desktop\salingeg\src\txt\hello.txt > C:\Desktop\salingeg\dest\txt > integration.docC:\Desktop\salingeg\src\doc\integration.doc > C:\Desktop\salingeg\dest\doc> UG.doc C:\Desktop\salingeg\src\doc\UG.doc C:\Desktop\salingeg\dest\doc >Applications.xml C:\Desktop\salingeg\src\xml\Applications.xml > C:\Desktop\salingeg\dest\xml > Platforms.xml C:\Desktop\salingeg\src\xml\Platforms.xml > C:\Desktop\salingeg\dest\xml > avc.alias C:\Desktop\salingeg\src\cnx\alias\avc.alias > C:\Desktop\salingeg\dest\cnx\alias > cats.alias C:\Desktop\salingeg\src\cnx\alias\cats.alias > C:\Desktop\salingeg\dest\cnx\alias > avc.init C:\Desktop\salingeg\src\cnx\init\avc.init > C:\Desktop\salingeg\dest\cnx\init > cats.init C:\Desktop\salingeg\src\cnx\init\cats.init > C:\Desktop\salingeg\dest\cnx\init > > > PYTHON SCRIPT: > > import xlrd, sys, os, shutil > > file_location = "C:\Users\salingeg\Desktop\input.xls" > workbook= xlrd.open_workbook(file_location) > sheet = workbook.sheet_by_index(0) > sheet.cell_value(0, 0) > for row in range(sheet.nrows): > source = [] > source.append(sheet.cell_value(row, 1)) > destination = [] > destination.append(sheet.cell_value(row, 2)) > files = [] > files.append(sheet.cell_value(row, 0)) > for f in files: > for s in source: > for d in destination: > print f > print s > print d > if (os.path.exists("d\\f")): The following line will either always be executed if you have a subdirectory "d" in your current working directory and that directory contains a file called "f"(unlikely) or never if "d\\f" doesn't exist (likely). Have a look at os.path.join() for the right way to join a directory with a filename into a path.. Use the interactive interpreter to make sure you get the desired result and understand how it works before you fix your script. > print ('File exists') > else: > shutil.copy(s, d) > > I am getting the following error: > > a..c > C:\Desktop\salingeg\src\code\a.c > C:\Desktop\salingeg\dest\code > Traceback (most recent call last): > File "C:\Users\salingeg\Desktop\excel_1.py", line 24, in <module> > shutil.copy(s, d) > File "C:\Program Files (x86)\python26\lib\shutil.py", line 84, in copy > copyfile(src, dst) > File "C:\Program Files (x86)\python26\lib\shutil.py", line 50, in > copyfile > with open(src, 'rb') as fsrc: > IOError: [Errno 2] No such file or directory: > u'C:\\Desktop\\salingeg\\src\\code\\a.c' According to the error message thefile you are trying to copy doesn't exist. Have a look into the C:\Desktop\salngeg\src\code folder, and check whether a file called a.c is there. If not you have three options - add the file - remove the line from the excel file - modify the code to check if the *source* file exists

Hi,


Satish ML wrote: [Regarding subject: let's see if we can trigger a bufferoverflow somewhere ;)] > On Wednesday, May 21, 2014 6:59:40 AM UTC+5:30, Rustom Mody wrote: >> On Tuesday, May 20, 2014 9:35:10 PM UTC+5:30, Jagadeesh N. Malakannavar >> wrote: > Hi Satish, > > Can you please send python part in plain text >> format? Python code here is > > difficult to read. It would be helpful to >> read >> https://wiki.python.org/moin/GoogleGroupsPython#Posting_from_Google_Groups >> Note particularly the 2 standard expectations: - Dont top post - Dont use >> excessively long (> 70 chars) lines > > Hi, > > Here is the code. > > > xls file looks as follows: > a.c C:\Desktop\salingeg\src\code\a.c C:\Desktop\salingeg\dest\code > hello.txt C:\Desktop\salingeg\src\txt\hello.txt > C:\Desktop\salingeg\dest\txt > integration.docC:\Desktop\salingeg\src\doc\integration.doc > C:\Desktop\salingeg\dest\doc> UG.doc C:\Desktop\salingeg\src\doc\UG.doc C:\Desktop\salingeg\dest\doc >Applications.xml C:\Desktop\salingeg\src\xml\Applications.xml > C:\Desktop\salingeg\dest\xml > Platforms.xml C:\Desktop\salingeg\src\xml\Platforms.xml > C:\Desktop\salingeg\dest\xml > avc.alias C:\Desktop\salingeg\src\cnx\alias\avc.alias > C:\Desktop\salingeg\dest\cnx\alias > cats.alias C:\Desktop\salingeg\src\cnx\alias\cats.alias > C:\Desktop\salingeg\dest\cnx\alias > avc.init C:\Desktop\salingeg\src\cnx\init\avc.init > C:\Desktop\salingeg\dest\cnx\init > cats.init C:\Desktop\salingeg\src\cnx\init\cats.init > C:\Desktop\salingeg\dest\cnx\init > > > PYTHON SCRIPT: > > import xlrd, sys, os, shutil > > file_location = "C:\Users\salingeg\Desktop\input.xls" > workbook= xlrd.open_workbook(file_location) > sheet = workbook.sheet_by_index(0) > sheet.cell_value(0, 0) > for row in range(sheet.nrows): > source = [] > source.append(sheet.cell_value(row, 1)) > destination = [] > destination.append(sheet.cell_value(row, 2)) > files = [] > files.append(sheet.cell_value(row, 0)) > for f in files: > for s in source: > for d in destination: > print f > print s > print d > if (os.path.exists("d\\f")): The following line will either always be executed if you have a subdirectory "d" in your current working directory and that directory contains a file called "f"(unlikely) or never if "d\\f" doesn't exist (likely). Have a look at os.path.join() for the right way to join a directory with a filename into a path.. Use the interactive interpreter to make sure you get the desired result and understand how it works before you fix your script. > print ('File exists') > else: > shutil.copy(s, d) > > I am getting the following error: > > a..c > C:\Desktop\salingeg\src\code\a.c > C:\Desktop\salingeg\dest\code > Traceback (most recent call last): > File "C:\Users\salingeg\Desktop\excel_1.py", line 24, in <module> > shutil.copy(s, d) > File "C:\Program Files (x86)\python26\lib\shutil.py", line 84, in copy > copyfile(src, dst) > File "C:\Program Files (x86)\python26\lib\shutil.py", line 50, in > copyfile > with open(src, 'rb') as fsrc: > IOError: [Errno 2] No such file or directory: > u'C:\\Desktop\\salingeg\\src\\code\\a.c' According to the error message thefile you are trying to copy doesn't exist. Have a look into the C:\Desktop\salngeg\src\code folder, and check whether a file called a.c is there. If not you have three options - add the file - remove the line from the excel file - modify the code to check if the *source* file exists

Hi,

Source file exists in the directory.
 
P

Peter Otten

Satish ML wrote:

[You're back to turd-formatted text; please find a permanent fix for this]
C:\Desktop\salingeg\dest\code > Traceback (most recent call last): > File
"C:\Users\salingeg\Desktop\excel_1.py", line 24, in <module> > shut
il.copy(s, d) > File "C:\Program Files (x86)\python26\lib\shutil.py",
line 84, in copy > copyfile(src, dst) > File "C:\Program Files
(x86)\python26\lib\shutil.py", line 50, in > copyfile > with open (src,
'rb') as fsrc: > IOError: [Errno 2] No such file or directory: >
u'C:\\Desktop\\salingeg\\src\\code\\a.c' According to the error message
the file you are trying to copy doesn't exist. Have a look into the
C:\Desktop\salngeg\src\code folder, and check whether a file called a.c
is there. If not you have three options - add the file - remove the line
from the excel file - modify the code to check if the *source* file
exists

Hi,

Source file exists in the directory.

If you are *really* sure about this (please double-check) it may be a rights
issue. Then make sure the user running the script has reading rights for the
file.
 
R

Rustom Mody

import xlrd, sys, os, shutil
for f in files:
for s in source:
for d in destination:
print f
print s
print d
if (os.path.exists("d\\f")):

Am I missing something very basic???

Should you be writing
"d\\f"
or
d ++ "\\" ++ f
?

[You're back to turd-formatted text; please find a permanent fix for this]

Satish:
Please see https://mail.python.org/pipermail/python-list/2014-May/672340.html
to get an idea how your posts look to others here

Then see
https://mail.python.org/pipermail/python-list/2014-May/
to see how most posts look
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top