Python Forum
Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows
#1
So, first, here is the code:

#!python3 C:\Users\Chris\AppData\Local\Programs\Python\Python37-32
import csv
import os
import time
import os.path


x=0

f = open('filename.csv', 'r')
csv_f = csv.reader(f)

for row in csv_f:
#following is a counter as a bug solver
  x=x+1
  print (x)
#this creates the file and names it the contents of the first column or whatever column you change it to
  file = open(row[0]+".txt", "w")
#this writes the content from the second column or whatever column you change it to.
  file.write(row[2])
  file.close()
#following is my attempt to figure out if files are being closed in the right directory
print('Stored in', os.path.dirname(os.path.abspath(file.name)))
What it is supposed to do is take the content of a cell in column 1 (or "0" if you prefer), open a file with those cell contents as the filename, then input the content of row 3 into the file and save/close the file in the script directory.
There is a bit of "bug" tracking I put in so I can see if it is actually running - and it outputs to the screen. That output looks like this:
Quote:1
2
3
4
5
6
7
8
9
10
11
12
13
Stored in C:\Users\Chris\Desktop\GELOE\Excel Test

If I run it in IDLE - it generates the output and the 13 files:
Quote:.txt 6/10/2019 6:39:42 PM
Aton archive.txt 1 KB 6/10/2019 6:41:56 PM
Brass Lamp Rebuild Red chade.txt 1 KB 6/10/2019 6:41:56 PM
Candle.txt 1 KB 6/10/2019 6:41:56 PM
Crafts.txt 6/10/2019 6:41:56 PM
Cookbook.txt 1 KB 6/10/2019 6:41:56 PM
File:Csv_Scraper.py 1 KB 6/10/2019 6:39:41 PM
Description.txt 1 KB 6/10/2019 6:41:56 PM
Flexo lamp.txt 1 KB 6/10/2019 6:41:56 PM
KoreanJapanese Lamp rebuild.txt 1 KB 6/10/2019 6:41:56 PM
Bride rebuild.txt 1 KB 6/10/2019 6:41:56 PM
Oak and iron music stand.txt 1 KB 6/10/2019 6:41:56 PM
Photograph of Sulpher Springs.txt 1 KB 6/10/2019 6:41:56 PM
filename.csv 1 KB 6/10/2019 6:41:43 PM
iLamp.txt 1 KB 6/10/2019 6:41:56 PM
x-mas orniments.txt 6/10/2019 6:41:56 PM

If I double click or run it in CMD or run it in python within the CMD window (with the prompt) it generates the print output - including the last path line - but no files. As you can likely tell from the imports and odd shebang I have played with it a good bit. I should also add I am/was a py2 user and am now a py3 user (or trying to be). It's as if it is putting the new files someplace else and I cannot find them. Or somehow running without closing them or throwing an error.

Appreciate any thoughts. Hope I was clear - first post.
Reply
#2
When no path is given open('filename.csv', 'r') files will be in folder you run script.
Remove that shebang,i don't use it Windows or Linux.
Run normal don't double click.
Look here for basic setup Python 3.6/3.7 and pip installation under Windows
This so python and pip work from anywhere in cmd or better cmder.

Can show the basic if make some changes and run it.
with open automatically close file-object,and indentation is 4-space in Python
# read_test.py
import csv

''' filename.csv
1,a
2,b
3,c
4,d
'''

with open('filename.csv') as f:
    csv_f = csv.reader(f)
    for row in csv_f:
         with open(f'{row[0]}.txt', 'w') as f_out:
            f_out.write(row[1])
Running from command line:
E:\div_code\read
# Test that python work
λ python -V
Python 3.7.3

# ls or same as dir(cmd)
E:\div_code\read
λ ls
filename.csv  read_test.py

# Run
E:\div_code\read
λ python read_test.py

# ls or same as dir(cmd)
E:\div_code\read
λ ls
1.txt  2.txt  3.txt  4.txt  filename.csv  read_test.py

# look at file cmd(type)
E:\div_code\read
λ cat 1.txt
a # 2.txt will have b ect...
As you see all is done in folder that i made E:\div_code\read.
Reply
#3
Thanks for the reply and time.

I had just fixed an issue with PIP, Commodo was hanging it. That should be fixed now.
I had just tried "with" - no help - but did it a bit different than you.
Per your comment - I removed the whole shebang.

So I copied your code. Your code works just like mine - meaning it will write and close the files - but only when run in IDLE.

To be clearer (I hope), there are four ways (possibly more) to start a script in Windows. 1) In IDLE. 2) Double click - which opens the CMD window and runs it. 3)In the CMD as a type of executable (i.e. C:\proper\path>file.py) or 4)C:\proper\path>py file.py, and likely some other way to pass it to the CMD window py prompt. All of these ways will run code that ask for input and has calculations or text output. However, only in IDLE (IDE) can I seem to get files to close.

It's late here though and I am tired - so I will try again tomorrow. I know I have done this before in 2.x - I probably misconfigured something when I had the pip problem. Or - maybe COmmodo is stopping the file closes?

Thank you again.
Reply
#4
Well,

I can't figure it out. Tried 2 machines. Python scripts will work just fine in the CMD window as far as calculations and printing to screen, etc...
However, when I try to do anything with files in the CMD window, the script appears to parse fine - but won't produce files - like text files - even when I use *with*. But the scripts will work fully and make .txt files if I run them from the Shell.

I wonder....is this the problem?

In CMD

Quote:>>> sys.path
['', 'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
>>>

In Python Shell
Quote:>>> sys.path
['C:\\Users\\Chris\\Desktop\\GELOE\\Excel Test',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\Lib\\idlelib',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\python37.zip',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\lib',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32',
'C:\\Users\\Chris\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\site-packages']
>>>

There are missing paths in the CMD and an empty path. That's odd. And How would I fix that? path has always vexed me.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can I not see the code correctly in Python IDLE.? Trump 8 657 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
  Produce One file Per PurchaseOrder jland47 1 328 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  hi need help to make this code work correctly atulkul1985 5 771 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 981 Nov-15-2023, 06:56 PM
Last Post: jst
  newbie question - can't make code work tronic72 2 675 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Rename files in a folder named using windows explorer hitoxman 3 733 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Code works but doesn't give the right results colin_dent 2 709 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Beginner: Code not work when longer list raiviscoding 2 816 May-19-2023, 11:19 AM
Last Post: deanhystad
  Phyton Opening files on windows C: pc EddieG 3 981 Mar-29-2023, 03:19 PM
Last Post: buran
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,773 Jan-29-2023, 06:24 PM
Last Post: Melcu54

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020