os.listdir() shows nothing while it is not so - python

I am trying to sort my files for school automatically but when I try this:
import os, sys
path = "/Sorting for School/"
dirs = os.listdir(path)
#print all dirs
for file in dirs:
print file
Although there are two txt documents in dir but when I run this the output is:
[]
Thanks

The code is fine. You must be querying a wrong path.
/dir/ means a subdirectory named dir in the root directory (in UNIX) or in the root directory of the current drive (in Windows).

Your code is fine. The following should work too:
import os
path = "/Sorting for School/"
def handle_err(err):
print err
for root,dirs,files in os.walk(path,onerror=handle_err):
for name in files:
print(name)

Related

Extract full Path and File Name

Attempting to write a function that walks a file system and returns the absolute path and filename for use in another function.
Example "/testdir/folderA/222/filename.ext".
Having tried multiple versions of this I cannot seem to get it to work properly.
filesCheck=[]
def findFiles(filepath):
files=[]
for root, dirs, files in os.walk(filepath):
for file in files:
currentFile = os.path.realpath(file)
print (currentFile)
if os.path.exists(currentFile):
files.append(currentFile)
return files
filesCheck = findFiles(/testdir)
This returns
"filename.ext" (only one).
Substitute in currentFile = os.path.join(root, file) for os.path.realpath(file) and it goes into a loop in the first directory. Tried os.path.join(dir, file) and it fails as one of my folders is named 222.
I have gone round in circles and get somewhat close but haven't been able to get it to work.
Running on Linux with Python 3.6
There's a several things wrong with your code.
There are multiple values are being assigned to the variable name files.
You're not adding the root directory to each filename os.walk() returns which can be done with os.path.join().
You're not passing a string to the findFiles() function.
If you fix those things there's no longer a need to call os.path.exists() because you can be sure it does.
Here's a working version:
import os
def findFiles(filepath):
found = []
for root, dirs, files in os.walk(filepath):
for file in files:
currentFile = os.path.realpath(os.path.join(root, file))
found.append(currentFile)
return found
filesCheck = findFiles('/testdir')
print(filesCheck)
Hi I think this is what you need. Perhaps you could give it a try :)
from os import walk
path = "C:/Users/SK/Desktop/New folder"
files = []
for (directoryPath, directoryNames, allFiles) in walk(path):
for file in allFiles:
files.append([file, f"{directoryPath}/{file}"])
print(files)
Output:
[ ['index.html', 'C:/Users/SK/Desktop/New folder/index.html'], ['test.py', 'C:/Users/SK/Desktop/New folder/test.py'] ]

Find a file 1 folder level down

I am trying to get full_path of places.sqlite file present in '%APPDATA%\Mozilla\Firefox\Profiles\<random_folder>\places.sqlite' using Python OS module. The issue as you can see that <random_folder> has a random name and there could be multiple folders inside the Profiles folder.
How do I navigate/find the path to the places.sqlite file?
You would ideally want to go through each folder to search for this file. In terminal 'locate file_name' command would do this for you. In python file you can use the following command:
import os
db_path = os.path.join(os.getenv('APPDATA'), r'Mozilla\Firefox\Profiles')
def find_file(file_name, path):
for root_folder, directory, file_names in os.walk(path):
if file_name in file_names:
return os.path.join(root_folder, file_name)
print(find_file('places.sqlite', db_path))
os.walk gives a list of all files in a path recusivly. Use it to search for 'places.sqlite' as follows.
path = ""
for root, dirs, files in os.walk("%APPDATA%\\Mozilla\\Firefox\\Profiles\\"):
if "places.sqlite" in files:
path = os.path.join(root, 'places.sqlite')
break
Use the os module to list out all directories in %APPDATA%\Mozilla\Firefox\Profiles\
loop over the directories until you find places.sqlite file (also using os module)
A glob might be simpler as in this case one expects the file to be there in level below the Profiles folder or not there at all.
import os
import pathlib
profiles = pathlib.Path(os.environ["APPDATA"]) / "Mozilla" / "Firefox" / "Profiles"
# rglob will recursively search as well
if places := list(profiles.rglob("places.sqlite")):
print(places[0]) # will print the sqllite file path
with places[0].open() as f:
# ....

my program of searching file does not search folder inside folder

I have made a file search program which search for the file. it is working fine with searching in current working directory and also inside one folder, however it does not work folder inside folder, I am not getting why? Can anyone please help?
My Code:
import os
files = []
def file_search(file, path=""):
if path == "":
path = os.getcwd()
for item in os.listdir(path):
if os.path.isdir(item):
path = os.path.realpath(item)
file_search(file, path)
elif item == file:
files.append(item)
return files
print(file_search("cool.txt"))
I think it will be simpler if you used glob library.
Example:
import glob
files = glob.glob('**/cool.txt', recursive=True)

Python: How to move list of folders (with subfolders) to a new directory

I have a directory that literally has 3000+ folders (all with subfolders).
What I need to be able to do is to look through the directory for those files and move them to another folder. I've done some research and I see that shutil is used to move files, but i'm unsure how to input a list of files to look for.
For example, in the directory, I would like to take the following folders (and their subfolders) and move them to another folder called "Merge 1"
1442735516927
1442209637226
1474723762231
1442735556057
1474723762187
1474723762286
1474723762255
1474723762426
1474723762379
1474723762805
1474723762781
1474723762936
1474723762911
1474723762072
1474723762163
1474723762112
1442209642695
1474723759389
1442735566966
I'm not sure where to start with this so any help is greatly appreciated. Thanks!
Combining os and shutil, following code should answer your specific question:
import shutil
import os
cur_dir = os.getcwd() # current dir path
L = ['1442735516927', '1442209637226', '1474723762231', '1442735556057',
'1474723762187', '1474723762286', '1474723762255', '1474723762426',
'1474723762379', '1474723762805', '1474723762781', '1474723762936',
'1474723762911', '1474723762072', '1474723762163', '1474723762112',
'1442209642695', '1474723759389', '1442735566966']
list_dir = os.listdir(cur_dir)
dest = os.path.join(cur_dir,'/path/leadingto/merge_1')
for sub_dir in list_dir:
if sub_dir in L:
dir_to_move = os.path.join(cur_dir, sub_dir)
shutil.move(dir_to_move, dest)

All Files in Dir & Sub-Dir

I would like to find all the files in a directory and all sub-directories.
code used:
import os
import sys
path = "C:\\"
dirs = os.listdir(path)
filename = "C.txt"
FILE = open(filename, "w")
FILE.write(str(dirs))
FILE.close()
print dirs
The problem is - this code only lists files in directories, not sub-directories. What do I need to change in order to also list files in subdirectories?
To traverse a directory tree you want to use os.walk() for this.
Here's an example to get you started:
import os
searchdir = r'C:\root_dir' # traversal starts in this directory (the root)
for root, dirs, files in os.walk(searchdir):
for name in files:
(base, ext) = os.path.splitext(name) # split base and extension
print base, ext
which would give you access to the file names and the components.
You'll find the functions in the os and os.path module to be of great use for this sort of work.
This function will help you: os.path.walk() http://docs.python.org/library/os.path.html#os.path.walk

Categories