Suppose that I have a working directory called directory 1. This directory has an app.py file and a directory file called directory2. Inside directory2 I have a text file called example.txt.
Is there a way of finding the path of example.txt using a command in app.py, knowing only the name of the text file and that it exists inside some directory of my working directory?
OS.walk() is what you are after.
Demo
import os
def find_file():
for root, dirs, files in os.walk(os.getcwd()):
if 'example.txt' in files:
path = os.path.join(root, 'example.txt')
print(f'file exists at {path}')
return
print('file does not exist')
find_file()
Related
How can I extract all the .zip files in a certain directory to its parent directory?
I tried:
import zipfile
parent_directory = '../input'
directory = '../input/zip'
for f in os.listdir(directory):
with zipfile.ZipFile(os.path.join(directory,f), "r") as z:
z.extractall(parent_directory)
However the unzipped files are not saved in '..input/zip', they are saved in nested folders
This might be a bit exaggerated.
After files are unzipped, I run this to:
move the original .zip file up one directory level. (to avoid /src_filename' already exists error)
move all files from all subdirectories into the zip parent directory.
move the original .zip file back into the parent directory.
import os
import shutil
src = r'C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir\unzip_test2'
dest = r'C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir'
pdir = '../PyUnzip01'
os.replace(r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir\unzip_test2.zip", r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\unzip_test2.zip")
for root, subdirs, files in os.walk(src):
for file in files:
path = os.path.join(root, file)
shutil.move(path, dest)
os.replace(r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\unzip_test2.zip", r"C:\Users\Owner\Desktop\PythonZip\PyUnzip01\child_dir\unzip_test2.zip")
I am trying to collect all files with all sub-directories and move to another directory
Code used
#collects all mp3 files from folders to a new folder
import os
from pathlib import Path
import shutil
#run once
path = os.getcwd()
os.mkdir("empetrishki")
empetrishki = path + "/empetrishki" #destination dir
print(path)
print(empetrishki)
#recursive collection
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
for name in files:
filePath = Path(name)
if filePath.suffix.lower() == ".mp3":
print(filePath)
os.path.join
filePath.rename(empetrishki.joinpath(filePath))
I have trouble with the last line of moving files: filePath.rename() nor shutil.move nor joinpath() have worked for me. Maybe that's because I am trying to change the element in the tuple - the output from os.walk
Similar code works with os.scandir but this would collect files only in the current directory
How can I fix that, thanks!
If you use pathlib.Path(name) that doesn't mean that something exists called name. Hence, you do need to be careful that you have a full path, or relative path, and you need to make sure to resolve those. In particular I am noting that you don't change your working directory and have a line like this:
filePath = Path(name)
This means that while you may be walking down the directory, your working directory may not be changing. You should make your path from the root and the name, it is also a good idea to resolve so that the full path is known.
filePath = Path(root).joinpath(name).resolve()
You can also place the Path(root) outside the inner loop as well. Now you have an absolute path from '/home/' to the filename. Hence, you should be able to rename with .rename(), like:
filePath.rename(x.parent.joinpath(newname))
#Or to another directory
filePath.rename(other_dir.joinpath(newname))
All together:
from pathlib import os, Path
empetrishki = Path.cwd().joinpath("empetrishki").resolve()
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
root = Path(root).resolve()
for name in files:
file = root.joinpath(name)
if file.suffix.lower() == ".mp3":
file.rename(empetrishki.joinpath(file.name))
for root, dirs, files in os.walk(path, topdown=True, onerror=None, followlinks=True):
if root == empetrishki:
continue # skip the destination dir
for name in files:
basename, extension = os.path.splitext(name)
if extension.lower() == ".mp3":
oldpath = os.path.join(root, name)
newpath = os.path.join(empetrishki, name)
print(oldpath)
shutil.move(oldpath, newpath)
This is what I suggest. Your code is running on the current directory, and the file is at the path os.path.join(root, name) and you need to provide such path to your move function.
Besides, I would also suggest to use os.path.splitext for extracting the file extension. More pythonic. And also you might want to skip scanning your target directory.
I want to read data from specific files from a directory whose structure is as follows:
-Directory
-Subdirectory1
-AGreek.srt
-AEnglish.srt
-Subdirectory2
-BGreek.srt
-BEnglish.srt
-test.py
Where test.py is my script and I'm supposed to read only the english files from the Directory. This is my current script:
import os
substring = 'English.srt'
for root, subdirs, files in os.walk('Directory'):
for filename in files:
if substring in filename:
fp = open(os.path.abspath(filename))
# Further Action
However this is giving me the following error:
FileNotFoundError: [Errno 2] No such file or directory: '/home/coder/Spam/AEnglish.srt'
How do I resolve the error? (P.s.: Spam is the folder inside which Directory and test.py are located)
You need to use os.path.join(root,filename) to access the file.
Ex:
import os
substring = 'English.srt'
for root, subdirs, files in os.walk('Directory'):
for filename in files:
if substring in filename:
fp = open(os.path.join(root,filename))
# Further Action
Please help I am new in python. I have multiple files from different subfolder of a root directory with .wmv extension replaced with .mp4 and create a folder name base on the filename by removing other character starting at '#'. How can I insert the string 'DATA_' at the start of a folder name. All new created folders should starts with "DATA_". MP4 files will be moved in a new created folder name.
example:
Directory: D:\GE-J1N-2991
Files found located at:
D:\GE-J1N-2991\1-ZWC4\20160705051835547#DXV_Ch1.wmv
D:\GE-J1N-2991\2-QWD4\20160705051836647#DXV_Ch1.wmv
D:\GE-J1N-2991\2-QWD4\34-QWERD\20160705084433078#DXV_Ch1.wmv
New created folder:
D:\GE-J1N-2991\1-ZWC4\20160705051835547\
D:\GE-J1N-2991\2-QWD4\20160705051836647\
D:\GE-J1N-2991\2-QWD4\34-QWERD\20160705084433078\
It should be renamed like this:
D:\GE-J1N-2991\1-ZWC4\DATA_20160705051835547\20160705051835547#DXV_Ch1.mp4
D:\GE-J1N-2991\2-QWD4\DATA_20160705051836647\20160705051836647#DXV_Ch1.mp4
D:\GE-J1N-2991\2-QWD4\34-QWERD\DATA_20160705084433078\20160705084433078#DXV_Ch1.mp4
Code:
import os, glob, shutil, fnmatch
folder = 'D:\\GE-J1N-2991'
os.chdir(folder)
def locate_WMV(pattern, root=os.curdir):
for path, dir, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
for asf in locate_WMV("*.wmv"):
os.rename(asf, asf[:-4] + ".mp4")
for f in locate_WMV("*#DVX_Ch1.mp4"):
new_dir = f.rsplit('#', 1)[0]
os.mkdir(new_dir)
shutil.move(f, os.path.join(new_dir, os.path.basename(f)))
If i use this code, the folder is renamed but created in Directory: D:\GE-J1N-2991 which is wrong, it should be created in where the file was found
new_dir2 = 'DATA_'+ f.rsplit('#', 1)[0].strip()[20:]
I would like to:
Write a script that takes a single directory path as command line argument, and then walks all subdirectories of that path looking for files with the extension '.py', copying each one to a temporary directory in your file system (eg /tmp/pyfiles). Your script should check for the existence of the temporary directory, and remove it if it already exists; it should then create a new directory, before beginning to copy files.
I have this:
#!/usr/bin/env python
import os, sys
import shutil
#import module
rootdir = sys.argv[1]
#take input directory
if os.path.exists('tmp/pyfiles'):
shutil.rmtree('tmp/pyfiles')
if not os.path.exists('tmp/pyfiles'):
os.makedirs('tmp/pyfiles')
#check whether directory exists, if it exists remove and remake, if not make
for root, dirs, files in os.walk(rootdir):
for f in files:
if os.path.splitext(f)[1] in ['.py']:
shutil.copy2(f, tmp/pyfiles)
#find files ending with .py, copy them and place in tmp/pyfiles directory
I get this error:
Traceback (most recent call last):
File "seek.py", line 20, in <module>
shutil.copy2(f, tmp/pyfiles)
NameError: name 'tmp' is not defined
Could anyone help me out?:) Thx
Your code says shutil.copy2(f, tmp/pyfiles), I believe it meant to be
shutil.copy2(f, 'tmp/pyfiles').
When you use the
os.walk()
method you loose track of the file full path. What I would do is to analyze each directory using the
os.listdir()
method and then copying each file taking into account its absolute path. Something like this:
for root, dirs, files in os.walk(rootdir):
for dir in dirs:
for f in os.listdir(dir):
if os.path.splitext(f)[1] in ['.py']:
shutil.copy2(os.path.join(root, dir, f), "tmp/pyfiles")
I hope this helps, maybe there is a cleaner solution.
You have to check if the root directory exists and walk in to remove everything if not create a new one.
To copy from dir to yours you have to check for files in dir that filename ends with .py, then replace the dir path with root path and create a new file in root dir with the content of matching file.
If we found a directory in dir we should create a new one in the root directory.
After that just call the function recursively to copy all content of dir to the root directory
import os, sys
rootdir = sys.argv[1]
PATH = "/tmp/pyfiles/"
def check(path, _file):
global rootdir, PATH
for item in os.listdir(path):
newpath = os.path.join(path, item)
if os.path.isdir(newpath):
os.mkdir(os.path.join(PATH, newpath.replace(rootdir, '')))
check(newpath, _file)
else:
if item.endswith(_file):
source = open(newpath, 'r')
print os.path.join(path, newpath.replace(rootdir, ''))
output = open(os.path.join(PATH, newpath.replace(rootdir, '')), 'w')
output.write(source.read())
output.close()
source.close()
if __name__ == '__main__':
if os.path.isdir(PATH):
for root, dirs, files in os.walk(PATH, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
os.rmdir(PATH)
os.mkdir(PATH)
check(rootdir, '.py')