-2

I am using Notepad++ as an editor and I am running Python 3 from Notepad. This is the code:

import sys

def write():
    print ("Creating new file")
    name = 'NewFile.txt'
    file = open(name,'w')
    file.close()
write()

The problem is not the code itself, I think. When I run the code from Windows PowerShell like this: python code.py, it works fine and creates the file, but this is creating the file using Python 2.7. I need to use Python 3.

When I run Python 3 from Notepad++ the file won't be created.

I tried running Python 2.7 from Notepad++ but it just doesn't work. I run it like this:

C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)"

or with Python 3 I run it like this:

C:\Python35\python.exe -i "$(FULL_CURRENT_PATH)" .

I also run Notepad++ as administrator.

I think I could solve this by running Python 3 alongside Python 2 in PowerShell, but I don't know how and the answers to these questions do not work for me:

I am open to changing my editor (Notepad++) or any solutions really.

So, how can I make Notepad++ create a new file? Or How can I make Python 3 run in PowerShell? Or Which editor could I use to fix this? Or maybe my code is just wrong.

Edit: When I say it doesn't work I mean that the file will not be created even tough my code runs (no error msg).

To be clear, what you're describing seems to be: a) when you run your file manually from the command prompt, it gives the expected results; b) when you run it instead through Notepad++, you don't see any evidence that it runs at all. Is that right?.

Yes, that's right. I'm also not sure what is the interactive interpreter.

Community
  • 1
  • 1
Cezzxar
  • 1
  • 2
  • 2
    What do you mean "doesn't work"? Does your program throw an error? Does a kitten dance across your screen? Be specific. – Etan Reisner Oct 27 '15 at 22:22
  • To be clear, what you're describing seems to be: a) when you run your file manually from the command prompt, it gives the expected results; b) when you run it instead through Notepad++, you don't see any evidence that it runs at all. Is that right? – lvc Oct 27 '15 at 22:40
  • In particular, since you're already running Python with `-i`, does the interactive interpreter appear when you run it from notepad++? – lvc Oct 27 '15 at 22:55
  • What result do you get when you run both statements without `-i` from PowerShell? Your code works just fine for me in both Python 2.7 and 3.5. – Ansgar Wiechers Oct 27 '15 at 23:41
  • When I run it without the -i it just dosn't create the file. – Cezzxar Oct 27 '15 at 23:44

2 Answers2

0

I think the problem is related to the encoding of your input file. When you open the file, put an encoding option to match your editor.

Alex J
  • 92
  • 1
  • 9
0

Most likely the file is being created, but not in the place where you expect it. You define the file without a path, so it's created in the current working directory. Check the program directory or the system directory for the file.

Specify output files with the full path to avoid this issue:

import sys

def write():
    print ("Creating new file")
    name = 'C:/path/to/NewFile.txt'
    file = open(name,'w')
    file.close()
write()
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328