Creating directories in Python

0

I have a list of directories to be created that have this structure:

Bla / Bla / Bla1.md

Bla / Bla / Bla2.md

Blu / Blue.md

Ble.md

However, the .md files should not be created, but the folders that contain them should, and for that I have made the following script:

# -*- coding: utf-8 -*-

import os


def build ( address ) :

    address = '/home/bezerk/Imagens/' + address

    if not os.path.exists ( address ) :

        os.makedirs ( address )


with open ( 'Arquivo.txt', 'r' ) as file :

    for line in file.readlines () :

        if line.endswith ( '.md' ) :

            # Tratamento aqui

            build ( line [:-3] )

        else :

            build ( line )

But I do not know why python does not recognize this Bla / Bla / Bla1.md but a separate item like this here Ble.md it recognizes, if anyone can give me insights to solve I'm grateful

    
asked by anonymous 16.07.2017 / 04:56

1 answer

1

Try to do the tests below and if you get the same results, the error is in your file handling logic, if the results are different there is something rotten in the OS realm :-) go to the command line: / p>

$ cd ~
~$ python

from os import makedirs as mkdir
d1 = 'teste1/bla/bla/bla.md'
mkdir(d1)

Exit the pyton and run the command tree to see the tree created from test1.

tree teste1  

Gobacktopythonanddo:

d2='test2/bla/bla/bla.md'mkdir(d2)

Exitpythonandtrytorunthecommandtreetogetthedirectorytreetest2andyouwillgetanerror:

$treeteste2teste2[erroropeningdir]

Whydoesthishappen?forthereasonthat,infact,thereisnodirectorycalled"test2" but "test2" (with a space after 2), then command tree should be executed as follows:

$ tree teste2\ 

Note that after the bar there is a space, this command results in the figure below:

Treegeneratedbymakedirswith"teste2"

I ran the tests on a ubuntu 16, maybe your problem is in the spaces between the bars, unless this is mandatory, try removing them, spaces in directory names can be a problem, if you have to it reminds you that you will have to use scape to reference them.

    
16.07.2017 / 17:10