473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to create file with spaces

hi

i have a dir that contains directories with names and spaces in between
example

rootdir
| ----> ABC DEF A
| ---> BDD SD N

I wanted to touch a file with the same name as the directories inside
each directory

rootdir
| ----> ABC DEF A
|-------> ABC DEF A-dummy
| ---> BDD SD N
|-------> BDD SD N-dummy

heres the code :
for d in os.walk(rootdir):
(dirpath, dirnames, filenames) = d
for dir in [dirpath]:
if not os.path.exists( os.path.join(dir,"-dummy") ):
f = open( os.path.join(dir,"-dummy") ,
"w").write("")

but i got only "-dummy" as the filename in each directory

How can i deal with spaces in this case? or is there some wrong things
i do in the code?
thanks for any help.

Apr 6 '06 #1
5 3223
s9************@yahoo.com wrote:
I wanted to touch a file with the same name as the directories inside
each directory

rootdir
| ----> ABC DEF A
|-------> ABC DEF A-dummy
| ---> BDD SD N
|-------> BDD SD N-dummy

heres the code :
for d in os.walk(rootdir):
(dirpath, dirnames, filenames) = d
for dir in [dirpath]:
if not os.path.exists( os.path.join(dir,"-dummy") ):
f = open( os.path.join(dir,"-dummy") ,
"w").write("")

but i got only "-dummy" as the filename in each directory


os.path.join joins path elements, so that's entirely expected.

try

filename = dirpath + "-dummy"
if not os.path.isfile(filename):
open(filename, "w").close()

instead. or if you want to "touch" the file even if it already exists,
just do

open(dirpath + "-dummy", "w").close()

(to only touch a file if it exists, you can use os.utime(filename, None)
instead)

</F>

Apr 6 '06 #2
> try

filename = dirpath + "-dummy"
if not os.path.isfile(filename):
open(filename, "w").close()


better make that

basename = os.path.basename(dirpath) + "-dummy"
filename = os.path.join(dirpath, basename)
if not os.path.isfile(filename):
open(filename, "w").close()

</F>

Apr 6 '06 #3
Alle 18:18, giovedě 06 aprile 2006, s9************@yahoo.com ha scritto:
How can i deal with spaces in this case?

I don't have an idea with python, but if can help I may say that bash you
might use "\ " to escape a space or use a quoted full path.
The shell program "basename" is failing, anyhow with file names containing
spaces.

F
Apr 6 '06 #4
Fulvio wrote:
Alle 18:18, giovedě 06 aprile 2006, s9************@yahoo.com ha scritto:
How can i deal with spaces in this case?

I don't have an idea with python, but if can help I may say that bash you
might use "\ " to escape a space or use a quoted full path.
The shell program "basename" is failing, anyhow with file names containing
spaces.

F


Python works fine with directories AND files with spaces in them.
Your problems are somewhere else.

-Larry Bates
Apr 6 '06 #5
dirpath is just a string, so there's no sense in putting it in a list
and then iterating over that list.

If you're trying to do something with each file in the tree:

for dir, subdirs, names in os.walk(rootdir):
for name in names:
filepath = os.path.join(dir, name) + "-dummy"
if not os.path.exists(filepath):
f = open(filepath, 'w').write('')

Apr 6 '06 #6

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

Similar topics

14
by: Jeff | last post by:
Ok. I am creating a table in an access DB based on a form submission. This works fine. The problem I am having is, if there is a space in the submission. For example.... someone submits "The...
13
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. ...
1
by: Amy L. | last post by:
I am working on creating a web service that will create a signed xml file. I would like to create this file in memory and than somehow email it to the user that requested it to be generated. I...
6
by: windandwaves | last post by:
Hi Folk Some of my clients asked me to create "fancy emails" for them (aka html formatted emails). I know how to make a nice html document, but I had trouble creating a simple way to provide...
0
by: simpleeshelbee | last post by:
When I went back and looked at what came out to see for my message, I saw that it came out a little different than I need it. I need it like this.... * = asterisks 1st row = 1 *, 9 spaces, 10...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
2
by: Gasnic | last post by:
Hi I need to create a .NET object that will be serialized into the following xml structure ( I can't change the XML schema) <Root> <Record> <Field1>data1</Field1> <Field2>data2</Field2>...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
4
by: nano2k | last post by:
Hi For some purposes, I need to create a new binary file and to allocate a fixed length for that file on the disk. I need something like File.Create(string fileName, long initialSizeInBytes)....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.