Python Forum
Trying something that may not be possible?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying something that may not be possible?
#1
OK, buran helped my in another thread and I am immensely grateful.

Now I want to get the file names (including their path) on my hard drive. I have researched and researched and came up with something that I thought would work.

Here is my feeble attempt at doing a windows command prompt for the directory of C: hoping to use DIR command.
subprocess.run(["C:\\windows\\system32\\cmd.exe", "/C", "dir ", "c:*.*", " /S /O:N /B /L", "dirtest001.txt"],shell=True)

So, when running my script it gets to this and the error comes up "File not found" nothing else. Not sure what is wrong but...

Oh, some explanation:
I am trying to get the complete path and filename of the files on my hard drive and save it to a file. Using windows command I issue the command "dir c:\*.* /S /O:N /B /L >dirtest001.txt" and this works. Takes a while but works. "Porting" it into the subprocess.run command, well, I said it above.

Any help would be greatly appreciated.
Reply
#2
With subprocess you don't have to run "C:\\windows\\system32\\cmd.exe

https://docs.python.org/3/library/subprocess.html
Reply
#3
It should be correct but tried not using the full path...didn't work.

Still trying to figure out what is wrong and the path forward .
Reply
#4
Like this works when i do a test.
import subprocess

subprocess.run('dir C:\\bar\*.* /S /O:N /B /L > out.txt', shell=True)
Reply
#5
So, I tried this using the suggestion from snippsat with a modification that I wanted to start from the root.

Here is what I used:
subprocess.run('dir c:\\*.* /S /O:N /B /L > directorytest0.txt')

and this is the "error" that came back:
dir: cannot access 'c:\*.*': No such file or directory
dir: cannot access '/S': No such file or directory
dir: cannot access '/O:N': No such file or directory
dir: cannot access '/B': No such file or directory
dir: cannot access '/L': No such file or directory
dir: cannot access '>': No such file or directory
dir: cannot access 'directorytest0.txt': No such file or directory

so, what am I doing wrong???? Completely stumped.....but at my age that is an easy task! Wall
Reply
#6
Why are you using subprocess in the first place, rather than Python facilities for file and directory access (like os.scandir, or pathlib)?
buran and snippsat like this post
Reply
#7
I addition to @ndc85430 comment - in the other thread I suggested using regedit via subprocess because you were not satisfied with other available options to work with Windows Registry.
What prevents using native python tools - os, pathlib or glob in this case?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Mar-17-2023, 02:43 AM)fredep57 Wrote: Here is what I used:
subprocess.run('dir c:\\*.* /S /O:N /B /L > directorytest0.txt')
You don't have shell=True in that code.
Testet work.
import subprocess

subprocess.run('dir c:\\*.* /S /O:N /B /L > directorytest0.txt', shell=True)
The problem can also be that don't have access/privilege of accessing to root C:\ partition.
Then need to run code Elevated(also as administrator)
Test with a folder first before doing whole C:\ drive.

As mention can do this with eg pathlib.
from pathlib import Path
from os import fspath

dest = 'C:\\foo'
with open('out1.txt', 'w', encoding='utf-8') as f:
    for path in Path(dest).rglob('*'):
        #print(path)
        f.write(f'{fspath(path)}\n')
It depend on use case when doing whole root of C:\(which is not a normal task) then subprocess can do the task better.
Reply
#9
Thanks all for the information and guidance.

I did research the os.system and found that that was deprecated a few years ago. Research said to use subprocess.run going forward so was focusing that. Maybe naive but that is the path I decided to pursue.

SO, my intent is to get the entire hard drive file names with their associated paths so that I can monitor what is installed and/or inadvertently installed on the system. I am not concerned about file sizes right now as some naturally grow/shrink as called.

So, I am still trying to figure out how (or if) I can use the subprocess to do what I want/need.

I did add, per snippsat's reply, the
shell = true
and that seems to be working. Thanks for finding that for me.

Just a side note. I am a neophyte when it comes to Python. I am still learning about it and trying to understand more and more as I go along. I came from an automation background but using a proprietary system with property scripting system so this is new to me. I will probably be asking more questions as I go along but I do try to research what and when I can. I usually get myself wrapped around the axle, so to speak though.

Again, thank you all for your generous time and help!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020