Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating files and directories via Python

I'm having trouble creating a directory and then opening/creating/writing into a file in the specified directory. The reason seems unclear to me. I'm using os.mkdir() and

path=chap_name print "Path : "+chap_path                       #For debugging purposes if not os.path.exists(path):     os.mkdir(path) temp_file=open(path+'/'+img_alt+'.jpg','w') temp_file.write(buff) temp_file.close() print " ... Done" 

I get the error

OSError: [Errno 2] No such file or directory: 'Some Path Name'

Path is of the form 'Folder Name with un-escaped spaces'

What am I doing wrong here?


Update: I tried running the code without creating the directory

path=chap_name print "Path : "+chap_path                       #For debugging purposes temp_file=open(img_alt+'.jpg','w') temp_file.write(buff) temp_file.close() print " ... Done" 

Still get an error. Confused further.


Update 2:The Problem seems to be the img_alt, it contains a '/' in some cases, which makes is causing the trouble.

So I need to handle the '/'. Is there anyway to escape the '/' or is deletion the only option?

like image 262
ffledgling Avatar asked Jul 28 '12 11:07

ffledgling


People also ask

Can Python create directories?

os. mkdir() method in Python is used to create a directory named path with the specified numeric mode.

How do you create a file inside a directory in Python?

To create a file inside a specific directory, we need to open a file using the absolute path. An absolute path contains the entire path to the file or directory that we need to use. It includes the complete directory list required to locate the file. For example, /user/Pynative/data/sales.

Can you create files with Python?

Python provides an inbuilt function for creating, writing, and reading files.

What are files and directories in Python?

A directory or folder is a collection of files and subdirectories. Python has the os module that provides us with many useful methods to work with directories (and files as well).


2 Answers

import os  path = chap_name  if not os.path.exists(path):     os.makedirs(path)  filename = img_alt + '.jpg' with open(os.path.join(path, filename), 'wb') as temp_file:     temp_file.write(buff) 

Key point is to use os.makedirs in place of os.mkdir. It is recursive, i.e. it generates all intermediate directories. See http://docs.python.org/library/os.html

Open the file in binary mode as you are storing binary (jpeg) data.

In response to Edit 2, if img_alt sometimes has '/' in it:

img_alt = os.path.basename(img_alt) 
like image 169
Rob Cowie Avatar answered Oct 11 '22 17:10

Rob Cowie


    import os     os.mkdir('directory name') #### this command for creating directory     os.mknod('file name') #### this for creating files     os.system('touch filename') ###this is another method for creating file by using unix commands in os modules  
like image 40
Surya Avatar answered Oct 11 '22 17:10

Surya