2

I know it's a noob question but I have some difficulties to make it works

def create(file):
    f = open(file,'w')

it returns "IOError: [Errno 2] No such file or directory: "

If I do that it works of course:

file ="myfile"
f = open(file,'w')

But I can't figure out how to create my file from the function parameter

Sorry for the noob question, thanks in advance for your help.

8
  • What do you pass to file at the first function? Also you are missing ' after the w Commented Jan 22, 2015 at 15:35
  • I pass a simple string. I tried file = str(file) before passing it to open it doesn't change anything Commented Jan 22, 2015 at 15:37
  • 1
    The reported error will occur is file (terrible name to use BTW) is a path including non-existent directories -- open doesn't auto-create such directories (see os.makedirs(os.dirname(file)) for that, and use try/except around it to catch the exception if the directory exists). Commented Jan 22, 2015 at 15:38
  • open do autocreate a file when I define it like that before file= "myfilename" that's what I don't understand. I don't want to create any directory, just a file Commented Jan 22, 2015 at 15:40
  • @François Richard 1. you should close the file at the end, 2. If you pass a string like "test" it will create the file at the current run folder, so you probably pass a folder path that doesn't exists. Commented Jan 22, 2015 at 15:40

3 Answers 3

2

when you pass the "http://somesite.com/" as file to your function python treats it as a directory structure.

As soon as python gets to "http:/" it presumes we have a directory. Using forward slashes in unix is not allowed and I imagine it is the same for windows.

To turn the name into something useable you can use some variation of urlparse.urlsplit:

import urlparse

import urlparse
def parse(f):
    prse  = urlparse.urlsplit(f)
    return prse.netloc if f.startswith("http") else prse.path.split("/",1)[0]
Sign up to request clarification or add additional context in comments.

Comments

2

Sites can look like paths to directories to the operating system. for instance: stackoverflow.com/something will be interpreted as a directory stackoverflow.com in which there is a file something.

You can see this when you use os.path.dirname:

>>> os.path.dirname('stackoverflow.com/something')
'stackoverflow.com'

If this is indeed the case, and you still want to proceed, you're passing a path to a location in a directory and not just a file name.

You have to make sure the directory stackoverflow.com exists first:

file_path = 'stackoverflow.com/something'
dirname = os.path.dirname(file_path)

if not os.path.exists(dirname):
    # if stackoverflow.com directory does not exist it will be created
    os.makedirs(dirname)

# .. carry on to open file_path and use it.

Watch out from http:// and the likes and consider using a real url parser.

tip: file is already defined in python, you shouldn't override it by using it to name a variable.

1 Comment

I just want to create a file
1

Editing:

def create(file):
    f = open(file,'w')
    f.close()

If you call this function using:

create('myfile.txt')

It will create a file named myfile.txt in whatever directory the code is being run from. Note that you are passing in a string not an object.

Since I now see you are passing in a string similar to http://www.google.com, you are trying to create a file named www.google.com in the http: folder. You are going to have to truncate or change the / since Windows files cannot contain that character in their names.

We'll use everything after the last / in this example:

def create(filename):
    filename = re.sub(r'.*//*', '', filename)
    f = open(filename, 'w')
    f.close()

So calling: create('www.google.com/morestuff/things') will create a file called things

2 Comments

I know this solution is working my problem is that it doesn't work when the file name come from the function parameter. def create(file): f = open(file,'w')
Okay, then we'll need to see more of your code so we know what you are passing into the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.