473,773 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

batch mkdir using a file list

Hello,
I think I've tried everything now and can't figure out how to do it.
I want to read in a text list from the current directory,
and for each line in the list, make a system directory for that name.

My text file would look something like this:
1144
1145
1146
1147

I simply want to create these 4 directories.
It seems like something like the following
code should work, but it doesn't.

import os

file = open("list.txt" , "r")
read = file.read()
print "Creating directory " + str(read)
os.mkdir(str(re ad))

Appreciate any help you can give!
R.D. Harles

Sep 23 '05 #1
7 3215
DataSmash <rd*@new.rr.com > wrote
I think I've tried everything now and can't figure out how to do it.
I want to read in a text list from the current directory,
and for each line in the list, make a system directory for that name.

My text file would look something like this:
1144
1145
1146
1147

I simply want to create these 4 directories.
It seems like something like the following
code should work, but it doesn't.

import os

file = open("list.txt" , "r")
read = file.read()
print "Creating directory " + str(read)
os.mkdir(str(re ad))


read() returns *all* text in the file as a single string, but you
really want to process each line for itself. try this:

for name in open("list.txt" ):
name = name.strip() # get rid of extra whitespace
os.mkdir(name)

</F>

Sep 23 '05 #2
DataSmash wrote:
Hello,
I think I've tried everything now and can't figure out how to do it.
I want to read in a text list from the current directory,
and for each line in the list, make a system directory for that name.

My text file would look something like this:
1144
1145
1146
1147

I simply want to create these 4 directories.
It seems like something like the following
code should work, but it doesn't.

import os

file = open("list.txt" , "r")
read = file.read()
print "Creating directory " + str(read)
os.mkdir(str(r ead))

Appreciate any help you can give!
R.D. Harles

Untested code:

import os
for line in open("list.txt" , "r"):
os.mkdir(line)
- JMJ
Sep 23 '05 #3

Jeremy Jones wrote:
DataSmash wrote:
Hello,
I think I've tried everything now and can't figure out how to do it.
I want to read in a text list from the current directory,
and for each line in the list, make a system directory for that name.

My text file would look something like this:
1144
1145
1146
1147

I simply want to create these 4 directories.
It seems like something like the following
code should work, but it doesn't.

import os

file = open("list.txt" , "r")
read = file.read()
print "Creating directory " + str(read)
os.mkdir(str(r ead))

Appreciate any help you can give!
R.D. Harles
Untested code:

import os
for line in open("list.txt" , "r"):
os.mkdir(line)


That won't work (in Windows at least) because you have
to remove the "\n" from the line before you pass it to
os.mkdir.



- JMJ


Sep 23 '05 #4
I am using bash shell on windows and I'm getting the error:
TypeError: loop over non-sequence

Is there any way around not messing with the text file.
I want to batch generate the text lists as well.

Thanks!

Sep 23 '05 #5
OR...if python can't handle this type of text file,
what do I need to do to remove the "\n" from the file?

Sep 23 '05 #6
DataSmash wrote:
I am using bash shell on windows and I'm getting the error:
TypeError: loop over non-sequence
Use open(filename). readlines() instead of open(filename) or switch to a
current Python version.
OR...if python can't handle this type of text file,
what do I need to do to remove the "\n" from the file?


line.strip() removes all leading and trailing whitespace (not just "\n")
from the line string.

import os
for line in open("list.txt" ).readlines():
directory = line.strip()
os.mkdir(direct ory)

Peter

Sep 23 '05 #7
Awesome! That worked!
Much thanks to Peter and all of you who took the time to answer my
question.
R.D.

Sep 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3560
by: Mark C. | last post by:
I'm trying to call a batch file that I've built using the FileSystemObject and CreateObject("Wscript.Shell"), oShell.Run... in an asp script. Naturally, I can get the script to work from a command line but not from a browser. The page does not throw an error but the oShell.Run... portion of the script doesn't run. Any help would be appreciated. Thanks.
0
2577
by: Steve Jorgensen | last post by:
I remember that I used to set up utility batch files, and create Windows shortcuts to them that would ask the user for parameters to supply to the batch files. From what I can tell, this functionality simply doesn't exist anymore in Windows 2000 or above. I did find a work-around to this that I thought y'all might want to know about. There are a number of new fetures in batch files available on Windows 2000 including an extended syntax...
3
4473
by: emman_54 | last post by:
Hi every one, I am trying to run a batch file using my asp.net application. I am using the Process class to run the batch file. When I run my web application, In the task manager, i could see cmd.exe with ASPNET as a user. But nothing happens. It can't execute the batch file. This is the code i am using to run the batch file:
5
9275
by: eoindeb | last post by:
I am trying to create a directory on Solaris using the mkdir() function. This works fine when I pass a string literal ("/etc/hosts") to mkdir, but if I try passing a directory pointer to mkdir, it returns a -1 error. The directory error works fine with fopen - does anyone know what I am doing wrong?? Here's a snippet of the code: int Check_Directory() //check for existence of directory - if not there, create the file { int status, ret...
1
24310
by: steve | last post by:
Hi all, Here's some work in progress that should allow you to run a batch file as a custom action in a VS deployment project. Yup I know you can use js or wsh, but the target may not have either.. Essentially it's just a wrapper for the Process class and a command interpreter. Warning, it only partly works. I had wanted to pass in (a) The name of the batch file (through "BatchFileName"), and
30
7710
by: MikeC | last post by:
Good People, I'm writing a backup utility that uses a chdir() to go into the source directory (in which the files reside that I want to back up), so I don't want to use chdir() to get into the target directory (where the backup copies will be kept, on another drive). I can successfully test whether the target directory exists or not using findfirst(), but if it doesn't, I wanted to create it using mkdir(), but it doesn't like the path...
0
1040
by: elegipcio | last post by:
Hello everyone i'm new to asp.net i'm looking to How to get values (Variables) pre Declared in web form & pass it to a batch file. coz i've to get this values to be added to the batch file to Automate the process of creating records & Zones in our DNS server -------------------------- So, as a simple example i'll suppose to create a new folder on drive C:\ the name of this folder will come from the asp.net web form. i searched & get...
1
945
by: Ahmad Sabry | last post by:
Thanks Phill W. Thanks RobinS it was solved .. i got Access is denied it was because of the Internet Explorer Security. Thanks again. Dim A As String Dim B As String A = TextBox1.Text B = TextBox2.Text Dim p As New System.Diagnostics.Process
8
24530
by: vj | last post by:
How do I do the following unix command: mkdir -m770 test with the os.mkdir command. Using os.mkdir(mode=0770) ends with the incorrect permissions. Thanks, VJ
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7463
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.