Unable to open file located inside subdirectory python - python

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

Related

How to get the path from content root in python?

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()

Move files from subfolders to another folder

I want to move all the files from multiple subdirectories to another folder in the same directory as the parent folder but get the following error:
FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/MyDrive/Dev/FaceRec/lfw/Emmit_Smith/Emmit_Smith_0001.jpg' -> '/content/drive/MyDrive/Dev/FaceRec/negatives/Emmit_Smith_0001.jpg'
this is my code:
for directory in os.listdir('/content/drive/MyDrive/Dev/FaceRec/lfw'):
for file in os.listdir(os.path.join('/content/drive/MyDrive/Dev/FaceRec/lfw', directory)):
path = os.path.join('/content/drive/MyDrive/Dev/FaceRec/lfw', directory, file)
new_path = os.path.join('/content/drive/MyDrive/Dev/FaceRec/negatives', file)
os.replace(path, new_path)
Thank you for the help in advance
Try the following:
import os
import glob
import shutil
src_path = '/content/drive/MyDrive/Dev/FaceRec/lfw'
dest_path = src_path.replace('lfw', 'negatives')
flist = glob.glob(os.path.join(src_path, '*/*'))
for fname in flist:
shutil.move(fname, fname.replace(os.path.dirname(fname), dest_path))
Why this is better:
The glob functions helps to avoid one for loop and also prevents from repeatedly joining the paths together
shutil.movecreates the destination directory for you if it does not exist. Also it can handle symlinks.
By putting the paths in variables we can minimize copy past errors

Loop over image filenames and split them

I'm trying to slice a bunch of images from a directory. Try to loop over them, but I'm getting FileNotFoundError: [Errno 2] No such file or directory: '45678.png'
Actually, that's the name of one of the files. This is my code:
import image_slicer
import os
indir = '/Users/data/h3'
for root, dirs, filenames in os.walk(indir):
for file in filenames:
if file.endswith('.png'):
image_slicer.slice(file, 4)
Dir is ok, I don't get why cannot find the file, when actually finds it as per the error message
You need to add the path to the filename when you open it with slice().
Try image_slicer.slice(os.path.join(dirpath, name), 4)

WindowsError: [Error 267] The directory name is invalid - Spyder 2.7

I am trying to print the file names in my directory using the following code:
dir_path = 'D:/#/#/#/#.json'
for filename in os.listdir(dir_path):
print filename
f = open(os.path.join(dir_path,filename), 'r')
but this error is displayed when running the code:
for filename in os.listdir(dir_path):
WindowsError: [Error 267] The directory name is invalid: 'D:/#/#/#/#.json/*.*'
I am unsure what the json/*.* means in the error message, I am new to Python so apologies if this question is vague.
Your question is unclear as to whether the directory D:/#/#/# contains any files in it other than JSON files, so I shall give two answers. Hopefully one of them will apply to you:
Directory contains only JSON files
In that case, simply remove the /#.json from the end of dir_path:
dir_path = 'D:/#/#/#'
for filename in os.listdir(dir_path):
print filename
f = open(os.path.join(dir_path,filename), 'r')
Directory contains JSON files and other files that you want to exclude
In this situation it's best to use the Python glob module.
The following should list all of the .json files in the folder D:/#/#/#:
import glob
dir_path = 'D:/#/#/#/*.json'
for filename in glob.glob(dir_path):
print filename
f = open(filename, 'r')
Note that filenames returned by glob.glob include the directory path, so we don't use os.path.join on them.
os.listdir lists all files in the directory you give it. However it seems you're not passing it the name of a directory, you're passing it the name of a file. How can it possibly list all the files in a directory if you don't give it a directory?

How to Insert String to add in subfolders name in Python

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:]

Categories