0

The idea is, I have various directories with a random number of files in each directory. Currently, I have a working script that I can copy into each dir and run and it will rename each file in that dir to my liking.

Here is my pain point: If I were to copy this script to another dir, I would have to change the path within my script so it renames the files in the correct directory. If I do not change the path in my script, it will rename files from the previous dir, which is not my intention.

My question is: can I create a dynamic reference in my script so that the script will update its own path so it will rename the files in the intended dir? This way, I will only have to copy the script to the new dir and run it.

I am using os.rename() to rename my files.

os.rename is using a path that has a prefixed 'r'. This is an example of how I am doing this with os.rename():

python
folder = r'M:\Python Projects\Rename Files\Repo\\'
source = folder + file_name 
destination = folder + d + " " + lookup_table[lookup_val] + ".pdf"
os.rename(source, destination)

I would like my folder variable to be dynamic. So for example when I copy the script over to another dir, folder would be the path of the current dir.

Brandon
  • 11
  • 4
  • "However, when I use os.getcwd(), I get a path without the prefixed 'r' which is causing some errors due to the wrong path format, I think?"—unlikely, since [raw strings are just regular strings](https://stackoverflow.com/a/4415585/354577). The `r` prefix only matters for string _literals_. How are you using these strings? Can you provide a concrete example and error message? – ChrisGPT was on strike Mar 01 '22 at 19:35
  • I'm not really sure what you are trying to ask for here. I believe what you are trying to get at is something along the lines of you have a file path and you are making and moving files to this particular path, but you want to only have to copy the file path once and dynamically reference different specific files within that file path? – ArchAngelPwn Mar 01 '22 at 19:40
  • @ArchAngelPwn I edited my post, I hope it makes more sense now. – Brandon Mar 01 '22 at 19:49
  • @Brandon, this edit doesn't show how you are using `os.rename()`. I still don't understand why you think raw strings have anything do to with this. Are you asking how to get files relative to the location of the script? – ChrisGPT was on strike Mar 01 '22 at 19:51
  • Are you looking for [How do I get the path and name of the file that is currently executing?](https://stackoverflow.com/q/50499/354577) In particular, [this answer](https://stackoverflow.com/a/57631907/354577)? – ChrisGPT was on strike Mar 01 '22 at 19:54
  • Yea sorry I'm still a little confused as well. I'm not sure how the os.rename() comes into play since you can just do "folder = os.getwdir()" to get the name of the current working directory of the script. Additionally, if you print "folder" from the code above it is already changed to the correct formatting to be used in other places in your code – ArchAngelPwn Mar 01 '22 at 20:01
  • Sorry, I am having such a hard time explaining this. Not sure why. Basically, I need to be able to get my path in the format with the 'r' prefix otherwise it does not work. Does that make sense? Like I can use os.getcwd() to update my folder variable, but how do I get it formatted like my folder variable example with the prefix 'r' and extra ending backslash? – Brandon Mar 01 '22 at 20:06
  • @Brandon, I still don't have any idea how raw strings come into this, but I'm pretty sure the duplicate I linked to in my last comment is what you're looking for. Please take a look. – ChrisGPT was on strike Mar 01 '22 at 20:42
  • Hi @Chris. I was able to figure it out. It seems as though I was overcomplicating things. os.getcwd() works just fine, for some reason I just needed to append '\\' to the end of my os.getcwd() – Brandon Mar 01 '22 at 20:51
  • @Brandon, `os.getcwd()` doesn't include a directory separator at the end. The problem likely stems from how you're joining your paths. Instead of using `dir + file`, consider using `pathlib.Path(dir) / file` or `os.path.join(dir, file)`. – ChrisGPT was on strike Mar 01 '22 at 20:56
  • Side note: `os.getcwd()` does _not_ return the path of the script. It returns the current working directory, the directory you are in when you _run_ the script. If you run this from another directory, e.g. like `python ../../path/to/script.py` you may find it doesn't do what you want. – ChrisGPT was on strike Mar 01 '22 at 20:59

1 Answers1

0

Use: os.getcwd()

From python-get-current-directory