考虑以下树:
bin\ [directory]
--- file.ext
bin\a [directory]
--- file.bin
--- file2.bin
--- file3.bin
--- anotherDir\ [directory]
-------------- image.png
-------------- image1.png
-------------- image2.png
-------------- image3.png
-------------- image4.png
bin\b [directory]
--- xyz.etc
--- xyz.etc
--- zyx.etc
--- deepDir\ [directory]
-------------- image.tif
-------------- image1.tif
-------------- deepestDir\ [directory]
------------------------- something.exe
------------------------- app.exe
我想要做的是获取(现在打印)最深的可用文件,然后获取其目录名称。
所以请考虑这个最深的目录:
-------------- deepestDir\ [directory]
------------------------- something.exe
------------------------- app.exe
首先,我想打印相对路径:
bin\b\deepDir\deepestDir
在那之后,下一个最深的将是:
--- deepDir\ [directory]
-------------- image.tif
-------------- image1.tif
所以打印:
[..]等依据主目录.
我已经尝试了几种可能性并最终确定了这一点:
# First, collect ALL files:
SEARCH_PATH = "E:\\project\\elohim\\"
for root, dirs, files in os.walk(SEARCH_PATH):
for file in files:
relativePath = os.path.relpath(root, SEARCH_PATH)
if relativePath == ".":
relativePath = ""
print 'File: {}'.format(os.path.join(relativePath, file))
# Then collect DEEPEST subdirectories
subDirs = []
for root, dirs, files in os.walk(SEARCH_PATH):
subDirs.append(os.path.relpath(root, SEARCH_PATH))
subDirs.sort(lambda x, y: cmp(x.count(os.path.sep), y.count(os.path.sep)), reverse=True)
for k in subDirs:
print 'Directory: {}'.format(k)
实际上它不是我想要的,但是它真的很接近(它首先搜索目录中的所有文件,然后搜索最深的子目录)。
(所以我现在所拥有的就是这样):
- image.png
- image1.png
- image2.png
- image3.png
- image4.png
- xyz.etc
- xyz.etc
- zyx.etc
- image.tif
- image1.tif
[..]
and then directories:
bin\b\deepDir\deepestDir
bin\b\deepDir
bin\b
bin
它不够清楚,让我知道,我会尽力解释。
答案 0 :(得分:1)
这可能对您有用:
a2 = c(5, 5, 3, 3, 3, 3, 1, 1, 2, 4, 4, 4)
你遍历树两次。没有必要。代码实际上创建了一个元组列表。每个元组包含深度,相对路径和文件名。
之后,列表将被排序为首先包含最深的文件夹。
然后,代码按深度和相对路径对文件进行分组。我使用a3 = c(4, 4, 4, 2, 3, 3, 3, 3, 1, 1, 5, 5)?
方法的 [Flags]
public enum ProcessAccessFlags : uint
{
All = 0x001F0FFF,
Terminate = 0x00000001,
CreateThread = 0x00000002,
VirtualMemoryOperation = 0x00000008,
VirtualMemoryRead = 0x00000010,
VirtualMemoryWrite = 0x00000020,
DuplicateHandle = 0x00000040,
CreateProcess = 0x000000080,
SetQuota = 0x00000100,
SetInformation = 0x00000200,
QueryInformation = 0x00000400,
QueryLimitedInformation = 0x00001000,
Synchronize = 0x00100000
}
public static string GetWindowText(IntPtr ControlHandle, IntPtr ProcessHandle)
{
int BufferSize = 256;
StringBuilder sb = new StringBuilder(BufferSize);
byte[] ControlNameByteArray = new byte[BufferSize];
long BytesRead = 0;
IntPtr BytesReadPointer = new IntPtr(BytesRead);
IntPtr RemoteProcessMemoryAddress = IntPtr.Zero;
IntPtr RemoteProcessHandle = IntPtr.Zero;
try
{
uint Message = RegisterWindowMessage("WM_GETCONTROLNAME");
RemoteProcessHandle = OpenProcess(ProcessAccessFlags.CreateThread |
ProcessAccessFlags.QueryInformation |
ProcessAccessFlags.VirtualMemoryOperation |
ProcessAccessFlags.VirtualMemoryWrite |
ProcessAccessFlags.VirtualMemoryRead, false, Process.Id);
RemoteProcessMemoryAddress = VirtualAllocEx(RemoteProcessHandle, IntPtr.Zero, new IntPtr(sb.Capacity), AllocationType.Commit, MemoryProtection.ExecuteReadWrite);
if (RemoteProcessMemoryAddress == IntPtr.Zero)
{
throw new Exception("GetWindowText: " + GetLastWin32Error());
}
SendMessage(ControlHandle, Message, new IntPtr(sb.Capacity), RemoteProcessMemoryAddress);
ReadProcessMemory(RemoteProcessHandle, RemoteProcessMemoryAddress, ControlNameByteArray, BufferSize, out BytesReadPointer);
string ControlName = Encoding.Unicode.GetString(ControlNameByteArray).TrimEnd('\0');
return ControlName;
}
finally
{
VirtualFreeEx(RemoteProcessHandle, RemoteProcessMemoryAddress, BufferSize, FreeType.Release);
}
}
方法。
从那里可以很容易地打印出你喜欢的东西和任何订单。