LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2022, 09:44 AM   #1
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 73
Blog Entries: 4

Rep: Reputation: 1
python3 file creation wrong place


good morning

i'm trying to understand something. python3, i wrote a tkinter python3 gui program to track my sugar and document when i take my insulin and what i eat.
everything was fine. until i switched operating systems, originally wrote the program on debian 11, cbpp11.
it worked as expected. then i installed arcolinuxb (openbox) installed my dependency the program works fine only thing is
my .csv files are being created and append to /h/user/*.csv and not where the program is at /h/user/.my_tools/python_log/*.csv


ls -la
Code:
/home/klein ls -la *.csv
-rw-r--r-- 1 klein klein 956 Oct 31 07:06 blood_sugar.csv
-rw-r--r-- 1 klein klein 575 Oct 31 08:33 food.csv
-rw-r--r-- 1 klein klein 972 Oct 31 07:06 insulin.csv
-rw-r--r-- 1 klein klein 704 Oct 31 07:05 pills.csv
Code:
/home/klein/.my_tools/python_log ls -la
-rwxr-xr-x 1 klein klein   4746 Oct 30 15:49 blood_sugar.csv
-rwxr-xr-x 1 klein klein   2538 Oct 25 21:11 daily_log_file.py
-rw-r--r-- 1 klein klein 163840 Oct 25 21:11 dexc_MX14106746.sqlite
-rwxr-xr-x 1 klein klein   3539 Oct 30 15:50 food.csv
-rwxr-xr-x 1 klein klein   4898 Oct 30 15:51 insulin.csv
-rwxr-xr-x 1 klein klein   3880 Oct 30 15:52 pills.csv
-rwxr-xr-x 1 klein klein   1230 Oct 30 15:44 pull_down.py
drwxr-xr-x 2 klein klein   4096 Oct 30 18:52 __pycache__

i deleated everything in __pycache__ , and restarted the program but it is doing the something.
i don't understand why or how to fix it.
it should be appending to the .cvs files in /home/klein/.my_tools/python_log but it isn't.
any help or direction would be appreciated.

Code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
from datetime import datetime, tzinfo
import pytz
import os
import pull_down

root = Tk()
root.title("log")
root ['bg'] = '#0059b3'
root.geometry( "400x200" )

def show01():
    label001.config( text = clicked001.get() )
    insulin = open("insulin.csv", "a")
    now = datetime.now()
    stamp01 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    insulin.write(f'{stamp01}')
    insulin.write(f'{clicked001.get()}\n')
    insulin.close()

clicked001 = StringVar()
clicked001.set("00 units novolog")

drop001 = OptionMenu( root ,clicked001,*pull_down.pop001)
drop001.place(x=20, y=20)

button001 = Button( root , text = "click Me" , command = show01 ).place(x=175, y=20)

label001 = Label( root , text = " " )
label001.place(x=275, y=20)

def show02():
    label002.config( text = clicked002.get() )
    food = open("food.csv", "a")
    now = datetime.now()
    stamp02 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    food.write(f'{stamp02}')
    food.write(f'{clicked002.get()}\n')
    food.close()

clicked002 = StringVar()
clicked002.set( "3 x eggs" )

drop002 = OptionMenu( root ,clicked002,*pull_down.pop002)
drop002.place(x=20, y=55)

button002 = Button( root , text = "click Me" , command = show02 ).place(x=175, y=55)

label002 = Label( root , text = " " )
label002.place(x=275, y=60)

def show03():
    label003.config( text = clicked003.get() )
    pills = open("pills.csv", "a")
    now = datetime.now()
    stamp03 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    pills.write(f'{stamp03}')
    pills.write(f'{clicked003.get()}\n')
    pills.close()

clicked003 = StringVar()
clicked003.set("pills")

drop003 = OptionMenu( root ,clicked003,*pull_down.pop003)
drop003.place(x=20, y=95)

button003 = Button( root , text = "click Me" , command = show03 ).place(x=175, y=95)

label003 = Label( root , text = " " )
label003.place(x=275, y=100)

def show04():
    label004.config( text = clicked004.get() )
    pills = open("blood_sugar.csv", "a")
    now = datetime.now()
    stamp04 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    pills.write(f'{stamp04}')
    pills.write(f'{clicked004.get()}\n')
    pills.close()

clicked004 = StringVar()
clicked004.set("    blood sugar")

entry1 = Entry (root,textvariable = clicked004, font=('calibre',10,'normal'))
entry1.place(x=20, y=130, width=100, height=25)

button004 = Button( root , text = "click Me" , command = show04 ).place(x=175, y=130)

label004 = Label( root , text = " " )
label004.place(x=275, y=135)


root.mainloop()
thank you
 
Old 10-31-2022, 10:48 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 23,677

Rep: Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184Reputation: 5184
At first glance it probably depends on how you are executing your program and what is the current working directory.
Your using a relative path to open the files i.e food = open("food.csv", "a")

More information is required. If you are running the program from a desktop shortcut you need to either set the path for the working directory or use absolute paths to the location of your csv files.
 
Old 10-31-2022, 11:47 AM   #3
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 73

Original Poster
Blog Entries: 4

Rep: Reputation: 1
made a menu entry in menu.xml
Code:
Openbox 3.6.1
obmenu3
/home/klein/.my_tools/python_log/daily_log_file.py
this worked perfect in Debian 11 cbpp11, and am i not correct python should look in the directory it is launched in first for files.
not in less they have changed something or compiled python differently in arch/arcolinuxb.

Code:
food = open("food.csv", "a")
should be changed to:

Code:
food = open("/home/klein/.my_tools/python_log/food.csv", "a")
or maybe
food = open("~/.my_tools/python_log/food.csv", "a")
import pull_down and it finds the file with out a path pull_down.py because it is in the same dir as the program, so this doesn't make seance to me.
 
Old 10-31-2022, 12:39 PM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,248

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
Every program has a current (or actual) dir, which can be set before you start it or also it can be changed inside. Relative paths will be evaluated using this current dir as starting point. Usually it is just equal to the current/actual path of the parent process. You can print it using pwd.
From the other hand this current dir is not related to the location of the executable or the python script, therefore if you wish to use that dir you need to chdir into it (within your python script).
 
1 members found this post helpful.
Old 10-31-2022, 01:37 PM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120

I expected the Python documentation for "open" to contain relevant information here, but all it says is:
Quote:
file is a path-like object giving the pathname (absolute or relative to the current working directory) of the file to be opened...
So, a few notes to hopefully make things clear:

Python's working directory is the directory you executed the script from - if you're in /home/user when you run "python3 scriptname" or "path/to/script.py" that's where Python's working directory will be.

In shell scripts (i.e. Bash/etc), the working directory is available via the "pwd" command (also the "$PWD" shell variable), and is changed with shell commands "cd" (or "pushd"/"popd")

In Python scripts, identifying the current working directory requires importing the "os" module and then calling os.getcwd(), or to change it use os.chdir(path)

If you want the working directory to be the script's own directory, we need to know where that is, and that functionality exists in the "sys" module, via "sys.path[0]" - which will either be the directory that contained the script, (or an empty string if not executing a file, or something else if the script happens to have modified it).

So to change Python's working directory to the same directory as the Python script, use:
Code:
os.chdir(sys.path[0])
Using that at the start of your script, instead of hard-coding the absolute path, will make the script more portable - or perhaps if you want a second test version of it in a different location. (You could also have the location of the file specified via a command line argument, then read it from sys.argv array.)

One final note, although Python inherits the working directory from its parent shell, changing the directory within Python does not result in the directory having changed outside of Python.

 
1 members found this post helpful.
Old 10-31-2022, 02:21 PM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 19,248

Rep: Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525Reputation: 6525
Quote:
Originally Posted by boughtonp View Post
I expected the Python documentation for "open" to contain relevant information here
This is just a wrapper, like a lot of other functions to the native c implementation: https://www.man7.org/linux/man-pages/man2/open.2.html (from this point of view).
Interestingly these are not well documented, or at least not easy to find that information.
 
1 members found this post helpful.
Old 10-31-2022, 04:57 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,701

Rep: Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062Reputation: 5062
The working directory is good to know about, but in this case I think you probably want the path to the script. Which is stored in a global called __file__.

Code:

https://stackoverflow.com/a/55988112/240515
 
1 members found this post helpful.
Old 10-31-2022, 05:03 PM   #8
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 73

Original Poster
Blog Entries: 4

Rep: Reputation: 1
thank you boughtonp and pan64
the problem arose when i lost the screen on my laptop lol, luckily i had vnc and openssh and i could get all my info offend it.
python2.xx openbox menu.xml would not open my script, at all. i had to use an bash script:

Code:
cd /home/klein/.my_tools/python_log
python daily_log_file.py
now on a new os my python script will run /home/klein/.my_tools/python_log/daily_log_file.py from menu.xml entry.
so i have made a bash script to get things working again, but i will research os.chdir(sys.path[0])
things change and sometimes i have a hard time keeping up lol.
 
Old 11-04-2022, 10:58 AM   #9
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 73

Original Poster
Blog Entries: 4

Rep: Reputation: 1
ok so i marked this solved with out really solving it. this is the code that corrects it properly.
one thing i noticed, Veryable name did not need $
the code below tells my program where the working dir is.
Code:
path = "/home/klein/.my_tools/python_log"
os.chdir( path )
the code below is where i put the code above in my program.
Code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

from tkinter import *
from datetime import datetime, tzinfo
import pytz
import os
import pull_down

path = "/home/klein/.my_tools/python_log"
os.chdir( path )

root = Tk()
root.title("log")
root ['bg'] = '#0059b3'
root.geometry( "400x200" )

def show01():
    label001.config( text = clicked001.get() )
    insulin = open("insulin.csv", "a")
    now = datetime.now()
    stamp01 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    insulin.write(f'{stamp01}')
    insulin.write(f'{clicked001.get()}\n')
    insulin.close()

clicked001 = StringVar()
clicked001.set("00 units novolog")

drop001 = OptionMenu( root ,clicked001,*pull_down.pop001)
drop001.place(x=20, y=20)

button001 = Button( root , text = "click Me" , command = show01 ).place(x=175, y=20)

label001 = Label( root , text = " " )
label001.place(x=275, y=20)

def show02():
    label002.config( text = clicked002.get() )
    food = open("food.csv", "a")
    now = datetime.now()
    stamp02 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    food.write(f'{stamp02}')
    food.write(f'{clicked002.get()}\n')
    food.close()

clicked002 = StringVar()
clicked002.set( "3 x eggs" )

drop002 = OptionMenu( root ,clicked002,*pull_down.pop002)
drop002.place(x=20, y=55)

button002 = Button( root , text = "click Me" , command = show02 ).place(x=175, y=55)

label002 = Label( root , text = " " )
label002.place(x=275, y=60)

def show03():
    label003.config( text = clicked003.get() )
    pills = open("pills.csv", "a")
    now = datetime.now()
    stamp03 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    pills.write(f'{stamp03}')
    pills.write(f'{clicked003.get()}\n')
    pills.close()

clicked003 = StringVar()
clicked003.set("pills")

drop003 = OptionMenu( root ,clicked003,*pull_down.pop003)
drop003.place(x=20, y=95)

button003 = Button( root , text = "click Me" , command = show03 ).place(x=175, y=95)

label003 = Label( root , text = " " )
label003.place(x=275, y=100)

def show04():
    label004.config( text = clicked004.get() )
    pills = open("blood_sugar.csv", "a")
    now = datetime.now()
    stamp04 = now.strftime('%Y-%m-%d, %H:%M:%S.%f, ')
    pills.write(f'{stamp04}')
    pills.write(f'{clicked004.get()}\n')
    pills.close()

clicked004 = StringVar()
clicked004.set("    blood sugar")

entry1 = Entry (root,textvariable = clicked004, font=('calibre',10,'normal'))
entry1.place(x=20, y=130, width=100, height=25)

button004 = Button( root , text = "click Me" , command = show04 ).place(x=175, y=130)

label004 = Label( root , text = " " )
label004.place(x=275, y=135)


root.mainloop()
 
Old 11-04-2022, 11:22 AM   #10
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,983

Rep: Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120Reputation: 2120
Quote:
Originally Posted by kleinde66 View Post
this is the code that corrects it properly.
That will work on your machine when the script is at the path you have hard-coded, but if the script is moved/copied the hard-coded value would need to be updated.

The variable "sys.path[0]" will also work, but does not require changing if the script's location changes.

 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python3 -V returns Python 2.7.12, can't get python3 to work. gene_ Ubuntu 15 03-18-2019 11:39 AM
[SOLVED] making python3.6.4 default python3 Astral Axiom Linux - Newbie 17 04-14-2018 11:55 AM
[SOLVED] how to start python3.6 interpreter just by typing python in terminal not python3.6 bmohanraj91 Linux - Newbie 4 05-10-2017 08:51 AM
After upgrade python3.4 to python3.5.1 , not able to install packages "request" though pip3 YOGESHAS87 Linux - Software 1 08-03-2016 11:38 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration