im trying to make a script which search some file in whole computer similar to Search in windows. I want to do it without any libraries.
I started with setting dir to begginng of disk and then checking how much dirs i have - i want to make a function that will search for new dirs in current dir so at the end ill have list of all dirs in disk. But when i try to found all files ending with ".txt", i get error message WindowsError5 Acces Denied. What am i doing wrong? Thanks
import os
os.chdir("\\.")
dir = os.listdir()
dirs = []
for x in dir:
if os.path.isdir(x):
dirs.append(x)
for y in dirs:
o = os.listdir(y)
for p in o:
if p.endswith(".txt"):
print(p)
input()
First, if you want to walk a directory tree, use os.walk instead of trying to build it yourself. And if you're trying to learn how something like os.walk works, the source code should be right there in os.py.
Second, you probably don't have access to your entire filesystem unless you run as Administrator, so you're going to get a bunch of Access Denied errors as you try to step through the directories you don't have access to. You have to use try/catch to deal with these errors in whatever way you find appropriate (e.g., print the error and move on to the next directory)
Third, this whole idea is probably misguided. Windows Desktop Search does not actually search your whole tree, it keeps a database and searches that, which is much faster (and also allows users to search into paths they couldn't reach directly—for example, you might have access to /Users/foo, but not to /Users, which means WDS can show you files in /Users/foo but your script cannot).
Fourth, this whole thing is much easier to do with the POSIX 'find' tool or… I forget the name, but there's a DOS-derived tool that also comes with Windows that does the same thing but not as flexibly. Either way, it's a one-liner shell/batch command instead of dozens of lines of Python.
Finally, the way you've written this, it's going to search the current drive, not all drives, which probably isn't what you want, is it?
You've hit a directory that you don't have permission to look in. So what. Catch the exception and continue the search.
Related
In python I am creating a tempdir (using tempfile.TemporaryDirectory), writing a few text files within it, and then (after processing the files) calling tempdir.cleanup(). The program works, but I'm wondering if there are any dangers to this that aren't immediately visible, especially when working with a large number of files.
Is it a bad practice? It certainly is. Is it dangerous? Depending on the name, it might get overwritten by another application. Or it might not.
The entire temp folder might be wiped between reboots (linux does this, not sure about windows). Or it might not. The user might delete files in the temp folder to free up space. Or they might not.
Generally, it's just bad practice and don't do it. If your file is non-temporary, then don't put it in the temp folder.
I'm using inotify_simple to get notifications from a directory of directories. I'm accessing a directory that has multiple sub-directories, looping through those sub-directories and wanting to use inotify within each directory. My goal for this program is to be notified anytime anything happens (in particular, a creation of a file) in one of the sub-directories.
My file structure:
-mainDir
-subDir1
-file1
-file2
-subDir2
-file3
-file4
...etc.
I'm looping through the directories in mainDir, and setting that path as the path for inotify to search in:
for directory in os.listdir(self.path):
new_path = os.path.join(self.path, directory)
new_curr = self.inotify.new_curr_file_notification(new_path)
New path values are exactly what I expect:
.../mainDir/subDir1
.../mainDir/subDir2
When passing in new_path into my function (which is the path to give inotify), I'm expecting inotify to only look in that directory. However, I'm getting notifications that files in other directories are causing the notification.
path for inotify .../mainDir/subDir1
Event(wd=1, mask=256, cookie=0, name='someFileInSubDir2')
flags.CREATE
Does anyone know why this is happening? And, if anyone has any suggestions to make this process easier/better, I'm all ears! Thanks!
I'm the author of inotify_simple, and since it doesn't have a method called new_curr_file_notification, I'm guessing that's that's something you wrote. Without seeing that method, or some more code that demonstrates how you're using the library exactly, I unfortunately can't give you any advice, as there's not enough information to see how you're using inotify_simple.
If you post a complete example, I will probably be able to tell what's going wrong.
Feel free to post a bug report on the project't github if it looks like there might be a bug.
Just a brief outline of what I'm doing: I'm trying to automate some pdf merging routine with python in a network directory, which involves copying, deleting and creating files at a specific network location. Apologies if my language is not very precise.
I'm coding on windows 7, using python 3.6. The program will need to be distributed on other machines, so local and temporary fixes will probably not help. The code I wrote is fully functional and works fine with all the local folders and files, however, now that I need to make use of it on the network, I am having some difficulties accessing the folder I need.
Here is what I have tried:
os.system("pushd " + "\\" + "\\netWorkDrive\Reports")
check_output("pushd " + "\\" + "\\netWorkDrive\Reports", shell=True)
pushd and popd work fine when entered just in the cmd, but when I do system calls through python, they just don't go through. I send a system call, and it runs correctly, but then when I "cd" a current directory, it shows that I'm still in my previous one. If done through the cmd manually, everything works as desired. I have googled the issue, but did not end up finding anything working/useful. I would really appreciate any suggestions, and let me know if I need to clarify my problem further.
Thank you!
I would not use pushd/popd in such a way, I would just include the full paths, including network paths in the paths of whatever file operation I need to do
However if I really need to change working directory, I would do this with python:
import os
original_working_directory = os.getcwd()
# do stuff
new_networked_directory = r'\\server\share\folder'
# change to the networked directory
os.chdir(new_networked_directory)
# do stuff
#changeback to original working directory
os.chdir(original_working_directory)
# do more stuff
There should be no need for "temp drives" or the like really.
I saw this answer - How can I search sub-folders using glob.glob module in Python? but I didn't understand what os.walk() was doing (I read the docs but it didn't quite make sense).
I'm really new to pathing and still trying to make sense of it.
I have a script that lives in - /users/name/Desktop/folder/ and I want to access some files in /users/name/Desktop/folder/subfolder/*.html
I tried glob.glob('/users/name/Desktop/folder/subfolder/*.html') but it returned an empty list. I realize this is what the previous person did and it didn't work (I was just hoping that glob had been updated!)
Any thoughts on how to do this?
Without any further information it's hard to say what the issue is. I tested your syntax, it works fine for me. Are you sure the extension is .html not .htm in your /users/name/Desktop/folder/subfolder/ directory?
Also, to further troubleshoot you can check what python can see in you directory by running:
os.listdir('/users/name/Desktop/folder/subfolder/')
This should get you started.
This is my first question.
My python script opens and reads from a present text file using the following simple funct:
open("config.ini", "r")
As this is a relative path it is supposed to work because config.ini is placed in the same directory like the script is when it is launched, that should be the current working dir.
In fact this works perfectly on all of my 3 linux boxes, but I have one user who demands support because he gets an error while opening config.ini. The error raises because
os.path.exists("config.ini")
returns false even if the file is there!
Trying to fix this problem we found out that the only way to make it work is to place config.ini in his home directory despite the supposed working directory is another.
Also, if my script tries to create a file in the present working directory, the file is always created in his home dir instead, and so I think that for some reason his working dir is always home!
How can I troubleshoot this problem? Maybe I could introduce absolute paths, but I am afraid that os.getcwd() would return the homedir instead of the correct one.
Should I maybe suggest this user to fix his machine in some way?
Sorry for this long question but english is not my first language and I am a beginner in coding, so have some difficulties to explain.
Thank you very much in advance! =)
Could it be that the user is executing your script from his home directory?
I.e. suppose the script is in:
/home/user/test/foo/foo.py
But the user calls it thus:
/home/user> python test/foo/foo.py
In this case, the "current directory" the script sees is /home/user.
What you can do is find out the directory the script itself resides in by calling this function:
import os
def script_dir():
return os.path.dirname(os.path.realpath(__file__))
It will always return the directory in which the script lives, not the current directory which may be different. You can then store your configuration file there safely.
Along the same lines as Eli Bendersky's suggestion, you might want to try:
os.path.exists(os.path.join(sys.path[0],"config.ini"))
since sys.path[0] should always be the directory in which the script resides.