Python Forum
[SOLVED] [Windows] Run "dir"?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Windows] Run "dir"?
#1
Question 
Hello,

I can't figure out how to run "dir" in Windows. At this point, I get "Output:ERROR:None":

CMD = "dir /AD /OD c:\\".rstrip()
CMD = r"dir /AD /OD c:".rstrip()
CMD = r"dir /AD /OD c:"
CMD = "dir /AD /OD c:"
print(CMD)

p = subprocess.run((['cmd.exe', '/c', 'dir', CMD]), text=True, stdout=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
if p.returncode != 0:
	output = f"ERROR:{p.stderr}"
else:
	output = f"OK: {p.stdout}"
blah = input(f"Output:{output}")
Does someone know?

Thank you.

--
Edit: As a work-around, don't bother with Windows's dir command:

import time, os

DIR=r"c:\temp"
dirs = [s for s in os.listdir(DIR) if os.path.isdir(os.path.join(DIR, s))]
for dir in dirs:
	print(dir, time.ctime(os.path.getmtime(dir)))
import time
from datetime import datetime, timezone
from pathlib import Path

paths = [subdir for subdir in Path(DIR).iterdir() if subdir.is_dir()]
paths = sorted(paths, key=os.path.getmtime)
for path in paths:
	stat_result = path.stat()
	modified = datetime.fromtimestamp(stat_result.st_mtime, tz=timezone.utc).strftime('%Y-%m-%d')
	print(f"{path} {modified}")
Reply
#2
This works for me on Windows 11.
import subprocess

process = subprocess.run(
    "dir c:\\", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW
)
if process.returncode:
    print(process.stderr.decode("utf-8"))
else:
    print(process.stdout.decode("utf-8"))
It also worked when the command is split up into arguments.
process = subprocess.run(
    ["dir", "c:\\"],
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE,
    creationflags=subprocess.CREATE_NO_WINDOW,
)
The magic sauce is probably setting shell=True.

Using pathlib is a better solution.
Winfried likes this post
Reply
#3
Thanks much.

I had to use a different page code to get the characters right:

else:
	#UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 219: invalid start byte
	#print(process.stdout.decode("utf-8"))
	print(process.stdout.decode("cp852"))
Reply
#4
(Jun-02-2026, 07:43 PM)Winfried Wrote: I had to use a different page code to get the characters right:
You could use the keyword argument encoding="cp852" in the call to subprocess.run(). Then process.stdout and process.stderr would already be unicode strings of type str.

I don't know how to obtain the cp852 from your Windows os.
« We can solve any problem by introducing an extra level of indirection »
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] [Windows] Change directory date? Winfried 2 1,477 May-11-2025, 04:24 PM
Last Post: Winfried
Question [SOLVED] [Windows] Is this right way to run a CLI app? Winfried 2 1,685 Nov-28-2024, 10:01 AM
Last Post: Winfried
  [SOLVED] [Windows] Fails reading strings with accents Winfried 1 2,139 Apr-23-2023, 05:27 PM
Last Post: Larz60+
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 3 4,012 Jan-17-2023, 09:28 PM
Last Post: markoberk
  [SOLVED] [Windows] Converting filename to UTF8? Winfried 5 6,749 Sep-06-2022, 10:47 PM
Last Post: snippsat
  SOLVED: best way to block (wait on) shell calls to multiple windows programs at once? ezdev 0 3,471 Dec-10-2017, 06:42 AM
Last Post: ezdev

Forum Jump:

User Panel Messages

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