Python, how to get the actual file location - python

Using python 3: How do I get the path of my files?
Please check the code below:
path =r'\\Desktop'
os.chdir(path)
for root, dirs, files in os.walk(path):
for txt_file in files:
if txt_file.endswith(".txt"):
txt_fileSh=txt_file.rstrip(".txt")
path_txt_file=os.path.abspath(txt_file)
print(path_txt_file)
What I get is
\\Desktop\a.txt
\\Desktop\b.txt
...
What I should get:
\\Desktop\Fig1\a.txt
\\Desktop\Fig2\b.txt
Help is very much appreciated.

You don't need to change directory. You can build the full path based on the root of the walk as follows:
path = r'\Desktop'
import os
for root, _, files in os.walk(path):
for file in files:
if file.endswith('.txt'):
print(os.path.join(root, file))

The documentation for abspath states that:
os.path.abspath(path)
Return a normalized absolutized version of the pathname path. On most
platforms, this is equivalent to calling the function normpath() as follows:
normpath(join(os.getcwd(), path)).
So, you are actually just joining your path with the filename.
You want to use the path to the current directory instead, which is the first of the three items returned by os.walk, the one you named root.
So, just replace
path_txt_file=os.path.abspath(txt_file)
with
path_txt_file = os.path.join(root, txt_file)

Related

rename files inside another directory in python

I'm working with python and I need to rename the files that I have inside a directory for example:
C:\Users\lenovo\Desktop\files\file1.txt
C:\Users\lenovo\Desktop\file\file2.txt
C:\Users\lenovo\Desktop\files\file3.txt
I have these 3 files inside the files folder, and I want to change the name of these, I have my script inside another folder: C:\Users\lenovo\Desktop\app\rename.py
I don't know if this is the problem but this is what I tried and it didn't work for me:
import os
directory = r'C:\Users\lenovo\Desktop\files'
count=0
for filename in os.listdir(directory):
count +=1
f = os.path.join(directory, filename)
if os.path.isfile(f):
os.rename(f, "new_file"+str(count))
UPDATE
the code simply deletes the original files and tries to create others inside the folder where I have the python script.
You need to prepend the directory to the new files
import os
directory = r'C:\Users\lenovo\Desktop\files'
count=0
for filename in os.listdir(directory):
count +=1
f = os.path.join(directory, filename)
new_f = os.path.join(directory, "new_file"+str(count)+".txt")
if os.path.isfile(f):
os.rename(f, new_f)
In general, when in doubt, it's best to use long/absolute path names when renaming/moving files. If you want to rename a file in its current directory, use the full path name in the target file name, as well. So, try changing the line:
os.rename(f, "new_file"+str(count))
to:
os.rename(f, os.path.join(directory, "new_file"+str(count)))
This absolute path will rename each file in its original directory. Otherwise, as you've experienced, relative file names are treated as relative to the directory of the executable.
Once you do the above, you'll probably want to do more tweaks to get a better result, but this should get you closer to your objective.
You used a relative path for the target filename, so the operating system based the path on the current working directory. That CWD was also your script path hints that you ran the program from your script path.
You could use os.path.join to make the path relative to your target directory. But you could also use pathlib
from pathlib import Path
directory = Path(r'C:\Users\lenovo\Desktop\files')
count = 0
for target in Path.iterdir():
if target.is_file():
target.replace(directory/f"newfile{count}")
count += 1

Rename certain subfolders in a directory using python

I have a folder structure that looks something like this:
/Forecasting/as_of_date=20220201/type=full/export_country=Spain/import_country=France/000.parquet'
and there are approx 2500 such structures.
I am trying to rename only certain subfolders, mainly export_country as exp_cty and import_country as imp_cty.
So far I tried this but it doesn't seem to work. I never had to deal with such complex folder structures and I'm a bit unsure how to go about it. My script is below:
import os
from pathlib import Path
path = r"/datasets/local/Trade_Data" # Path to directory that will be searched
old = "import_country*"
new = "imp_cty"
for root, dirs, files in os.walk(path, topdown=False): # os.walk will return all files and folders in thedirectory
for name in dirs: # only concerned with dirs since I only want to change subfolders
directoryPath = os.path.join(root, name) # create a path to subfolder
if old in directoryPath: # if the 'export_country' is found in my path then
parentDirectory = Path(directoryPath).parent # save the parent directory path
os.chdir(parentDirectory) # set parent to working directory
os.rename(old, new)
The code you have proposed has 2 issues:
The first one: if old in directoryPath: checks if the string import_country* is inside the path.
From your question I have understood that you would like to rename all directories that start with "import_country"
so you can use the startswith for that.
The second problem is os.rename(old, new) you are trying to rename directory with name import_country* which doesn't exist, instead you should use the name variable.
Here is your code with slightly changes that is working, please note that you must use topdown=False as you are renaming directories while walking through them:
import os
from pathlib import Path
path = "/datasets/local/Trade_Data"
old_prefix = "import_country"
new_name = "imp_cty"
for root, dirs, files in os.walk(path, topdown=False):
for name in dirs:
if name.startswith(old_prefix):
directoryPath = os.path.join(root, name)
parentDirectory = Path(directoryPath).parent
os.chdir(parentDirectory)
os.rename(name, new_name)

program to traverse directory structure in python

I need to create a program in which I have been given a directory path, in that directory there can be n number of tree type directory structure, and in any directory there can be any number of .py File. Some files were executed are some dnt. So I need to create a script in python that only run those files which are not executed till now. Can someone please suggest the way.
Please take a look at os.walk().
import os
directory = '/tmp'
for (dirpath, dirnames, filenames) in os.walk(directory):
# Do something with dirpath, dirnames, and filenames.
pass
The usual approach is to use os.walk and to compose complete paths using os.path.join:
import os
import os.path
def find_all_files(directory):
for root, _, filenames in os.walk(directory):
for filename in filenames:
fullpath = os.path.join(root, filename)
yield fullpath
if __name__ == '__main__':
for fullpath in find_all_files('/tmp'):
print(fullpath)
In my experience, dirnames return value of os.walk is rarely used, so I omitted it with _.
As for your question about files being executed or not -- I don't get it. Please explain.

Is there existing any utilities to get all absolute path of leaf file under a given directory?

os.listdir(path) can return all the files under the directory of the given path. I an wonder if there exists any utility function that can conduct deep traverse and return all the paths of leaf files under a given directory.
Sure! You are looking for os.walk:
import os
for root, dirs, files in os.walk(path):
for filename in files:
filename = os.path.join(root, filename)
print(filename)

in python how do I figure out the current path of the os.walk()?

So I let a user to set a path to a directory that may contain subdirectories (more levels), and files.
I use os.walk() in my code to scan the whole directory:
for root, subdirs, files in os.walk(thispath):
for myfile in files:
shutil.move(os.path.realpath(myfile), os.path.join(thispath,filename))
but "os.path.realpath(myfile)" instead of giving me the absolute path of "myfile" (I also tried "os.path.abspath(myfile)" but does the same thing basically), gives me the path from where the script is running; just like a os.chdir() with attached the myfile filename. basically os.path.realpath(myfile) = os.path.join(os.chdir(),myfile), whereas myfile is obviously in a any other random directory, so it shouldn't be.
When I try to move that file it says that it doesn't exist, and it's true, it is not in the path it goes to look.
How do I get the absolute path of the file I am walking on ("myfile")?
for root, subdirs, files in os.walk(this_path):
for my_file in files:
shutil.move(os.path.join(root, my_file), os.path.join(this_path, filename))

Categories

Resources