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)
4
  • you need the r in base_path because the forward slash Commented Oct 25, 2018 at 19:04
  • forward slashes aren't special characters, do they really need raw string literals? stackoverflow.com/questions/2081640/… . Backslashes sure but not forward slashes, right? Commented Oct 25, 2018 at 19:05
  • @clearshot66 I have tried including r' before bath_path as well but no luck. base_path = r'/Users/user/Desktop/' Commented Oct 25, 2018 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. Commented Oct 25, 2018 at 19:18

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks I realized where I was going wrong all this while.
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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.