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


Messages In This Thread
Code works in IDLE, appears to work in CMD, but won't produce files in CMD/Windows - by ChrisPy33 - Jun-11-2019, 02:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm trying to install python 3.11.11 on windows 10 - it doesn't work Petonique 2 1,693 Feb-04-2025, 05:42 PM
Last Post: snippsat
  Can't get graph code to work properly. KDDDC2DS 1 635 Sep-16-2024, 09:17 PM
Last Post: deanhystad
  Windows Disk Cleanup Code Help Needed nal2us2 3 1,029 Jul-19-2024, 04:03 AM
Last Post: deanhystad
  Executable file compiled by PyInstaller does not work on Windows 7 amusaber 1 1,698 Jul-11-2024, 02:59 PM
Last Post: DeaD_EyE
  I can't for the life of me get this basic If statement code to work CandleType1a 8 2,133 May-21-2024, 03:58 PM
Last Post: CandleType1a
  Why can I not see the code correctly in Python IDLE.? Trump 8 2,473 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
  Produce One file Per PurchaseOrder jland47 1 1,050 Jan-26-2024, 11:38 AM
Last Post: Larz60+
  hi need help to make this code work correctly atulkul1985 5 1,880 Nov-20-2023, 04:38 PM
Last Post: deanhystad
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 4,158 Nov-15-2023, 06:56 PM
Last Post: jst
  newbie question - can't make code work tronic72 2 1,495 Oct-22-2023, 09:08 PM
Last Post: tronic72

Forum Jump:

User Panel Messages

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