2

I am trying to copy all the contents of a folder from one directory to another, but the final destination path is outside of the current working directory that Python is searching though.

How do I find this location without explicitly stating it?

Here's what I have:

from Myro import *
import shutil
import os
import sys

def imageSync():
    path1 = os.path.abspath("5-1-15 upload")
    path2 = "C:\Users\Owner\Pictures\plsWork" 
    #path2 = sys.path.inset(0, 'C:\Users\Owner\Pictures\plsWork')

    listing = os.listdir(path1)

    for image in listing:
        shutil.copy2( os.path.join(path1, image), path2)

I guess my problem is similar to: Importing files from different folder in Python. But I tried those suggestions and received a Type Error, so I commented out my attempts that didn't work.

Thanks for the help!

Community
  • 1
  • 1
Alina DW
  • 111
  • 1
  • 17
  • I'm not sure I understand your problem. Is path1 or path2 the final destination path? Something makes me think your problem is the `\U` not being escaped into `\\U`. – Eugene K May 08 '15 at 14:31
  • @EugeneK path2 is my destination path – Alina DW May 08 '15 at 14:34
  • Try escaping every \ in your strings. \ is used as a path separator on Windows and is also the escape character in Python. Reference: http://learnpythonthehardway.org/book/ex10.html – Eugene K May 08 '15 at 14:35
  • what version of python? – Padraic Cunningham May 08 '15 at 14:36
  • @PadraicCunningham 3.1 – Alina DW May 08 '15 at 14:39
  • Ok then you have a serious problem with the U, use `r"C:\Users\Owner\Pictures\plsWork" ` – Padraic Cunningham May 08 '15 at 14:39
  • @EugeneK So should I use \\ instead of \? But I would still be stating the location – Alina DW May 08 '15 at 14:40
  • @AlinaDW Yes you have to do \\ to escape a \ so that it isn't interpreted as a special character. Or you can do what Padraic suggested which essentially disables escaping in the string. What is wrong with stating the location? The only problem should be that it's user specific. But it should execute on your machine. – Eugene K May 08 '15 at 14:43
  • @EugeneK I was hoping to be able to move images from various folders from one network to another, unless that's not possible? I was just trying to start a basic code to move some stuff within a local directory on my own computer just to get familiar with moving stuff – Alina DW May 08 '15 at 14:48
  • @AlinaDW haven't worked with networked drives in a bit, but I know Python has some issues. This might help out: http://developer.covenanteyes.com/unc-paths-with-python/ – Eugene K May 08 '15 at 14:51
  • @EugeneK Perfect, that link seems to summarize what I'm trying to do. Thanks again! – Alina DW May 08 '15 at 14:55

1 Answers1

0

The \U will casue an error:

In [2]: "\U"
  File "<ipython-input-2-d8a2543b5007>", line 1
    "\U"
        ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape

You should always use raw string r or forward /'s in your windows paths.

r"C:\Users\Owner\Pictures\plsWork" 

Or /:

"C:/Users/Owner/Pictures/plsWork" 

string literals

The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences. A prefix of 'u' or 'U' makes the string a Unicode string

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321