BBBruce
BBBruce

Reputation: 51

How to turn dir result into csv or json in python?

I want to write a python parser to turn dir result into csv or json.

For example,the following is the result of dir /a/s/od/ta.

磁碟區 C 中的磁碟沒有標籤。
 磁碟區序號:  123-1234

 C:\ 的目錄

2009/07/14  上午 11:20    <DIR>          PerfLogs
2009/07/14  下午 01:08    <JUNCTION>     Documents and Settings [C:\Users]
2018/03/14  下午 04:09    12,796,198,912 hiberfil.sys
2018/03/14  下午 04:09    17,061,601,280 pagefile.sys
2018/03/14  下午 04:16    <DIR>          Recovery
2018/03/14  下午 04:17    <DIR>          Users
2018/03/14  下午 04:17    <DIR>          $Recycle.Bin
2018/03/14  下午 04:42    <DIR>          Intel
2018/03/14  下午 05:46    <DIR>          Python27
2018/03/22  下午 01:19                40 87E27E492B63
2018/03/31  上午 11:08    <DIR>          py36
2018/05/04  下午 06:12    <DIR>          ProgramData
2018/05/04  下午 06:12    <DIR>          Program Files (x86)
2018/05/04  下午 06:15    <DIR>          Windows
2018/05/07  上午 10:17    <DIR>          System Volume Information
2018/05/07  上午 10:19    <DIR>          Config.Msi
2018/05/08  上午 10:39    <DIR>          Program Files
               3 個檔案  29,857,800,232 位元組

I want it can be like the following.

============================================================
|**name      |path  |lastaccess             |type**       |
|PerfLogs    |C:\   |2009/07/14 上午 11:20  |<DIR>         |
|hiberfil.sys|C:\   |2018/03/14  下午 04:09 |12,796,198,912|
|pagefile.sys|C:\   |2018/03/14  下午 04:09 |17,061,601,280|
|Recovery    |C:\   |2018/03/14  下午 04:16 |<DIR>         |
|Users       |C:\   |2018/03/14  下午 04:17 |<DIR>         |
|$Recycle.Bin|C:\    |2018/03/14  下午 04:17 |<DIR>         |
|Intel       |C:\   |2018/03/14  下午 04:42 |<DIR>         |
|Python27    |C:\   |2018/03/14  下午 05:46 |<DIR>         |
|87E27E492B63|C:\   |2018/03/22  下午 01:19 |40            |
|py36        |C:\   |2018/03/31  上午 11:08 |<DIR>         |
|ProgramData |C:\   |2018/05/04  下午 06:12 |<DIR>         |
|Windows     |C:\   |2018/05/04  下午 06:15 |<DIR>         |
|Config.Msi  |C:\   |2018/05/07  上午 10:19 |<DIR>         |
|ProgramFiles|C:\   |2018/05/08  上午 10:39 |<DIR>         |
===========================================================

Do you know any python library that I could use? Or,would you give me some advice if there were no library to use? Thanks you very much.

Upvotes: 2

Views: 92

Answers (2)

Ajax1234
Ajax1234

Reputation: 71461

You can use os.popen with re:

import os, re 
import csv
rows = [i.strip('\n') for i in os.popen('dir /a/s/od/ta')]
drive = re.findall('[A-Z]:', '\n'.join(rows))[0]+'\\'
directories = [re.split('\s{2,}|(?<=\d)\s|\s(?=\d)', i) for i in rows if re.findall('^\d+/\d+/\d+|\<[A-Z]+\>', i)]
with open('dir_results.csv', 'w') as f:
  write = csv.writer(f)
  write.writerows([['**name', 'path', 'lastaccess', 'type**']]+[[c]+[drive]+[' '.join(a)]+[b] for *a, b, c in directories]) 

Output:

**name,path,lastaccess,type**
PerfLogs,C:\,2009/07/14 上午 11:20,<DIR>
Documents and Settings [C:\Users],C:\,2009/07/14 下午 01:08,<JUNCTION>
hiberfil.sys,C:\,2018/03/14 下午 04:09,"12,796,198,912"
pagefile.sys,C:\,2018/03/14 下午 04:09,"17,061,601,280"
Recovery,C:\,2018/03/14 下午 04:16,<DIR>
Users,C:\,2018/03/14 下午 04:17,<DIR>
$Recycle.Bin,C:\,2018/03/14 下午 04:17,<DIR>
Intel,C:\,2018/03/14 下午 04:42,<DIR>
Python27,C:\,2018/03/14 下午 05:46,<DIR>
87E27E492B63,C:\,2018/03/22 下午 01:19,40
py36,C:\,2018/03/31 上午 11:08,<DIR>
ProgramData,C:\,2018/05/04 下午 06:12,<DIR>
Program Files (x86),C:\,2018/05/04 下午 06:12,<DIR>
Windows,C:\,2018/05/04 下午 06:15,<DIR>
System Volume Information,C:\,2018/05/07 上午 10:17,<DIR>
Config.Msi,C:\,2018/05/07 上午 10:19,<DIR>
Program Files,C:\,2018/05/08 上午 10:39,<DIR

Upvotes: 0

Yash Ghorpade
Yash Ghorpade

Reputation: 607

You can definitely use Pandas Library to create CSV files.

import pandas as pd

Then store your dataframe or columns & rows in a variable, Ex. df.

df.to_csv(file_name, index = False, sep=',')

Here, your values will be seperated by a comma (,). You can use "|" as your separator too.

Upvotes: 1

Related Questions