Simple list comprehension - python

I want a dictionary of files:
files = [files for (subdir, dirs, files) in os.walk(rootdir)]
But I get,
files = [['filename1', 'filename2']]
when I want
files = ['filename1', 'filename2']
How do I prevent looping through that tuple? Thanks!

Both of these work:
[f for (subdir, dirs, files) in os.walk(rootdir) for f in files]
sum([files for (subdir, dirs, files) in os.walk(rootdir)], [])
Sample output:
$ find /tmp/test
/tmp/test
/tmp/test/subdir1
/tmp/test/subdir1/file1
/tmp/test/subdir2
/tmp/test/subdir2/file2
$ python
>>> import os
>>> rootdir = "/tmp/test"
>>> [f for (subdir, dirs, files) in os.walk(rootdir) for f in files]
['file1', 'file2']
>>> sum([files for (subdir, dirs, files) in os.walk(rootdir)], [])
['file1', 'file2']

for (subdir, dirs, f) in os.walk(rootdir): files.extend(f)

files = [filename for (subdir, dirs, files) in os.walk(rootdir) for filename in files]

import os, glob
files = [file for file in glob.glob('*') if os.path.isfile(file)]
if your files have extensions, then even simpler:
import glob
files = glob.glob('*.*')

Related

How to traverse through all subfolders inside a folder for renaming using glob function? [duplicate]

I have a folder structure:
I am using os.walk(path) to get all the files from the "test" folder. I would like to all files except the folder "B" and the files inside it.
test (root-folder)
t1.txt
t2.txt
A
f.txt
B
f1.txt
C
f4.txt
list1 = ['A', 'C']
result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(path) for f in filenames if os.path.splitext(f)[1] == '.txt']
for items in result:
for fname in list1:
if fname in items.lower():
result.remove(items)
print(result)
I tried it, but it takes only the A and C. Not the files in main folder? Can you help? Where am i wrong?
Thank you
Possible solution is to use glob library:
import glob
dir_to_exclude = ['B', 'C']
files = glob.glob('**/*.txt', recursive=True)
files_paths = [_ for _ in files if _.split("\\")[0] not in dir_to_exclude]
files_names = [_.split("\\")[-1] for _ in files if _.split("\\")[0] not in dir_to_exclude]
print(f'List of file names with path: {files_paths}')
print(f'List of file names: {files_names}')
I think this should work
file_paths = []
forbidden_path = GetForbiddenPath()
for root, dirs, files in os.walk(path):
for name in files:
file_path = os.path.join(root, name)
if forbidden_path in file_path:
if os.path.splitext(file_path)[1] == '.txt':
file_paths += [file_path]

python get files recursively

I have a folder structure:
I am using os.walk(path) to get all the files from the "test" folder. I would like to all files except the folder "B" and the files inside it.
test (root-folder)
t1.txt
t2.txt
A
f.txt
B
f1.txt
C
f4.txt
list1 = ['A', 'C']
result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(path) for f in filenames if os.path.splitext(f)[1] == '.txt']
for items in result:
for fname in list1:
if fname in items.lower():
result.remove(items)
print(result)
I tried it, but it takes only the A and C. Not the files in main folder? Can you help? Where am i wrong?
Thank you
Possible solution is to use glob library:
import glob
dir_to_exclude = ['B', 'C']
files = glob.glob('**/*.txt', recursive=True)
files_paths = [_ for _ in files if _.split("\\")[0] not in dir_to_exclude]
files_names = [_.split("\\")[-1] for _ in files if _.split("\\")[0] not in dir_to_exclude]
print(f'List of file names with path: {files_paths}')
print(f'List of file names: {files_names}')
I think this should work
file_paths = []
forbidden_path = GetForbiddenPath()
for root, dirs, files in os.walk(path):
for name in files:
file_path = os.path.join(root, name)
if forbidden_path in file_path:
if os.path.splitext(file_path)[1] == '.txt':
file_paths += [file_path]

zip file is zipping to default project dir folder rather than file dir

my code is :
def zip_file(path, zip_file_name ,root):
ziph = zipfile.ZipFile(zip_file_name, 'w', zipfile.ZIP_DEFLATED)
ziph.write(path, arcname= os.path.join(root, os.path.splitext(zip_file_name)[0]))
#os.remove(path)
ziph.close()
def zip_dir(dir_to_zip):
for root, dirs, files in os.walk(dir_to_zip):
for curr_file in files:
fullFilePath = os.path.join(root, curr_file)
if not curr_file.endswith('.zip'):
zip_file(fullFilePath, "{}.zip".format(fullFilePath), root)
if __name__ == '__main__':
zip_dir('C:\\Users\\My_namne\\Desktop\\TEST')
I want it to zip the file to the original folder, but it zips to work dir of my project all of the files. How to change the dir of output archive? now it zip to my working directory and the zip the entire root to path
The first argument to zipfile.ZipFile should be the complete path to the archive you are creating. Make the change in zip_dir
def zip_dir(dir_to_zip):
for root, dirs, files in os.walk(dir_to_zip):
for curr_file in files:
fpath = os.path.join(root, curr_file)
zip_file(fpath, "{}.zip".format(fpath),root)
This will create an archive for each file in its directory. The archive will only contain the file itself, not the file's entire path.
import os, zipfile
def zip_file(filepath, zipfilepath ,curr_file):
with zipfile.ZipFile(zipfilepath, 'w', zipfile.ZIP_DEFLATED) as ziph:
ziph.write(filepath, arcname=curr_file)
def zip_dir(dir_to_zip):
for root, dirs, files in os.walk(dir_to_zip):
for curr_file in files:
if not curr_file.endswith('.zip'):
filepath = os.path.join(root, curr_file)
zipfilepath = "{}.zip".format(filepath)
zip_file(filepath, zipfilepath, curr_file)
def zip_file(filename):
zip_filename = os.path.splitext(filename)[0] + '.zip'
f = zipfile.ZipFile(zip_filename, 'w')
f.write(filename, os.path.basename(filename), zipfile.ZIP_DEFLATED)
f.close()
return zip_filename
def zip_dir(dir_to_zip):
for root, dirs, files in os.walk(dir_to_zip):
for curr_file in files:
fullFilePath = os.path.join(root, curr_file)
if not curr_file.endswith('.zip'):
zip_file(fullFilePath)
this works

Python os.walk() loop directories for attribute

I have a text file containing directory names on each line (1, 2, 3), how can I iterate through the text file and have the result inserted into os.walk?
So far, I have
import os
from os import listdir
dir_file = open("list.txt", "r")
for root, dirs, files in os.walk(dir_file):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
I have used a combination of while loops and for loops and if statements to try and perform this, but I've had no luck.
Any help?
Thanks
How about:
rdf = []
for fff in dir_file:
for r, d, f in os.walk(fff):
rdf.append(r, d, f)
I resolved this issue with:
import os
from os import listdir
dir_file = open("list.txt", "r")
for x in dir_file:
for root, dirs, files in os.walk(x.strip()):
for name in files:
print(os.path.join(root, name))
for name in dirs:
print(os.path.join(root, name))
Thanks :)

Python : Create a list of subdirectories

I try to put a listing of subdirectories of a directory into a list.
I use that code :
import os
for dirname, dirnames, filenames in os.walk("W:/test"):
# print path to all subdirectories first.
for subdirname in dirnames:
a= os.path.join(dirname, subdirname)
liste = []
liste.append(a)
print liste
The problem is that I have not all my subdirectories into my list "liste"
Would you have any solutions ?
Thanks
You need to call liste.append inside the loop.
import os
liste = []
for root, dirs, files in os.walk(path):
for subdir in dirs:
liste.append(os.path.join(root, subdir))
print(liste)

Categories

Resources