0

I have this below Python script that fetches a file from one location and copies that to another Target location. The below code works just fine if I define the paths with the absolute locations.

I am trying to rather define this using variables, which when done does not execute the script. There is no error that is thrown but the code does not seem to be executed.

Code:

Path_from = r'/Users/user/Desktop/report'
Path_to = r'/Users/user/Desktop/report'

for root, dirs, files in os.walk((os.path.normpath(Path_from)), topdown=False):
        for name in files:
            if name.endswith('{}.txt'.format(date)):
                print
                "Found"
                SourceFolder = os.path.join(root, name)
                shutil.copy2(SourceFolder, Path_to)

I want to change the code from

Path_from = r'/Users/user/Desktop/report'

to

base_path = /Users/user/Desktop/
Path_from = r'base_path/{}'.format(type)
Kevin Nash
  • 1,511
  • 3
  • 18
  • 37
  • you need the r in base_path because the forward slash – clearshot66 Oct 25 '18 at 19:04
  • forward slashes aren't special characters, do they really need raw string literals? https://stackoverflow.com/questions/2081640/what-exactly-do-u-and-r-string-flags-do-and-what-are-raw-string-literals . Backslashes sure but not forward slashes, right? – erik258 Oct 25 '18 at 19:05
  • @clearshot66 I have tried including r' before bath_path as well but no luck. base_path = r'/Users/user/Desktop/' – Kevin Nash Oct 25 '18 at 19:13
  • r'base_path/{}' is a raw string literal containing the same characters you used as a variable 'base_path', but it doesn't add the _value_ of `base_path` in the string. I'm not sure what `.format(type)` is supposed to mean but I'm just going to assume the last bit of your code example is was not faithfully copied into the question. – erik258 Oct 25 '18 at 19:18

2 Answers2

1

I would recommend you leave all the current working directory concerns to the user - if they want to specify a relative path, they can enter into the directory to which it relates before invoking the python and providing relative paths.

This is what just about every linux tool and program does - rarely do they take a 'base path', but rather leave the job of providing valid paths relative to the current directory ( or absolute ) to the user.

If you're dedicated to the idea of taking another parameter as the relative path, it should be pretty straightforward to do. Your example doesn't have valid python syntax, but it's close:

$ cat t.py
from os.path import join
basepath="/tmp"
pathA = "fileA"
pathB = "fileB"
print(join(basepath,pathA))
print(join(basepath,pathB))

note however that this prevents an absolute path being provided at script execution time.

You could use a format instead,

basepath="/tmp"
pathA = "fileA"
pathB = "fileB"
print( "{}/{}".format(basepath, pathA) )
print( "{}/{}".format(basepath, pathB) )

But then you're assuming that you know how to join paths on the operating system in question, which is why os.path.join exists.

erik258
  • 14,701
  • 2
  • 25
  • 31
0

If I'm reading this right, you could use pathlib, specifically pathlib.Path code would look like

from pathlib import Path
import re
import shutil

path_from = Path("/") / "Users" / "user" / "Desktop" # Better IMO
# path_from = Path("/Users/user/Desktop")
path_to = Path("/") / "Users" / "user" / "OtherDesktop"

datename = "whatever"

for x in path_from.glob("*.txt"):
    if re.search(r"{}$".format(datename), x.stem): # stem is whatever is before the extension 
    # ex. something.txt -> something

        shutil.copy(str(path_from / x.name), str(path_to / x.name))
Clayton J Roberts
  • 347
  • 1
  • 5
  • 19