Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Avoid hard-coded folderPath in the code (let users provide it)
#1
I am using Python 3.7, and would like to know if is there method/function to ask the user to provide a folderPath (then print or work further)?

Please see my code as .replace() is not working for me here:
#Declare Variable
filePath = ""
fileName = ""
fileFullPath = ""

filePath = input("Enter the file path: )
fileName = input("Enter the file name with extension (.txt): ")
fileFullPath = filePath.replace("\","\\") + "\\" + fileName

print("Full file address is: " + fileFullPath)
Quote
#2
The standard module pathlib will make your life easier
from pathlib import Path
file_path = Path(input("Enter the file path: ").strip())
file_name = input('Enter the file name with extension (.txt): ').strip()
file_full_path = file_path / file_name
print("Full file address is", file_full_path)
Quote
#3
Thank you for a quick reply! It's another way by importing some library.

However, I wanted to know if we could print the full file path/address just by my basic codes (without importing anything)? What happened to .replace() here or do I need something else or is it not possible in this scenario?

I have just started learning Python 3, and it is just a simple thought came to my mind. I googled it, but still unable to understand where is the issue/gap in my code.
Quote
#4
(Sep-06-2018, 05:13 AM)SunOffice024 Wrote: It's another way by importing some library.
It is not just another way, nor some library. The pathlib module is in python's standard library, it is automatically installed when you install python. It is the result of the work of the python team who studied all the dark corners of the way system paths are handled in the Windows operating system and others. I cannot imagine a reason for not using it, besides masochism. Don't reinvent the wheel.
Quote
#5
I have got you point!! Thank you for helping me!!! :) :)
Quote
#6
I do agree with @Gribouillis
Can show both way,as you should have used os.path.join() in your first code.
from os import path

file_path = 'C:\\foo'  #input("Enter the file path: ")
file_name = 'test.txt'  #input("Enter the file name with extension (.txt): ")
full_path =  path.join(file_path, file_name)

print(f"Full file address is: {full_path}")

#---------------------------------------------

from pathlib import Path

file_path = Path('C:\\foo') #Path(input("Enter the file path: ").strip())
file_name =  'test.txt' #input('Enter the file name with extension (.txt): ').strip()
file_full_path = file_path / file_name
print("Full file address is", file_full_path)
Output:
Full file address is: C:\foo\test.txt Full file address is C:\foo\test.txt
So the output is the "same" almost.
If look closer at it,so is the first one a string,
and second on a Path object that show it'a a Windows Path.
>>> full_path
'C:\\foo\\test.txt'

>>> file_full_path
WindowsPath('C:/foo/test.txt')
What if need that Path object should be a string?
The right way is to use os.fspath(path) and not str.
>>>import os

>>> os.fspath(file_full_path)
'C:\\foo\\test.txt'

>>> # Can also to it without import 
>>> file_full_path.__fspath__()
'C:\\foo\\test.txt'
Quote
#7
>>> print("@snippsat: It's awesome!! Thank you for sharing detailed knowledge for lifetime!!!")
@snippsat: It's awesome!! Thank you for sharing detailed knowledge for lifetime!!!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  ARCPY enter excel table into coded domain Just_another_lost_man 6 517 Feb-23-2019, 09:23 PM
Last Post: buran
  how to avoid a lot of 'if' statments ? witch 12 1,341 Aug-27-2018, 02:20 PM
Last Post: witch
  How to avoid "None"s in the output sylas 1 694 Jun-06-2018, 12:41 PM
Last Post: buran
  Is This Code Ok? How Can I Avoid Using Global Variables? digitalmatic7 9 1,751 Feb-15-2018, 09:32 AM
Last Post: DeaD_EyE
  Is it possible to avoid apostrophes ? sylas 5 1,390 Jul-19-2017, 11:51 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)