Python Forum
Facing Problem while opening a file through command prompt
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Facing Problem while opening a file through command prompt
#1
Hi Everyone,

Here is the code that I have written for reading a file through command prompt.
from sys import argv script, filename = argv txt = open(filename) print(f"Here's your file {filename}:") print(txt.read()) print("Type the filename again:") file_again = input("> ") txt_again = open(file_again) print(txt_again.read())
but while executing this program I am facing this error
Error:
txt = open(file.txt) AttributeError: 'str' object has no attribute 'txt'
please guide me through this problem
Reply
#2
When I try this it works fine.
Note: every line in your script (except first) is indented 1 space.
This might be the reason you are having problems.

Also, since argv is a list, you can use filename = argv[1]
to get the second argument.
BashBedlam likes this post
Reply
#3
The error is being generated by code other than what you uploaded.txt = open(file.txt)
Reply
#4
(Jan-29-2022, 03:03 PM)Larz60+ Wrote: When I try this it works fine.
Note: every line in your script (except first) is indented 1 space.
This might be the reason you are having problems.

Also, since argv is a list, you can use filename = argv[1]
to get the second argument.
I tried this way but it is showing list index out of range.
Reply
#5
# fs.py from sys import argv script, filename = argv txt = open(filename) print(f"Here's your file {filename}:") print(txt.read()) print("Type the filename again:") file_again = input("> ") txt_again = open(file_again) print(txt_again.read())
From command line i have two files in bar folder your code fs.py and test.txt.
Output:
C:\code\bar λ ls fs.py test.txt C:\code\bar λ fs.py test.txt Here's your file test.txt: hello world Type the filename again: > test.txt hello world
As you see the code is working.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to Randomly Print a Quote From a Text File When User Types a Command on Main Menu BillKochman 13 6,754 Dec-16-2025, 05:29 PM
Last Post: zodazy
  problem with print command in super() akbarza 5 2,850 Feb-01-2024, 12:25 PM
Last Post: deanhystad
  Facing issue in python regex newline match Shr 6 9,944 Oct-25-2023, 09:42 AM
Last Post: Shr
  problem in using input command akbarza 4 4,524 Oct-19-2023, 03:27 PM
Last Post: popejose
  What is the difference between Command Prompt and Sublimes yaoyao22 1 1,650 Jul-09-2023, 02:56 PM
Last Post: snippsat
  Facing problem with Pycharm - Not getting the expected output amortal03 1 1,857 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Dynamic File Name to a shared folder with open command in python sjcsvatt 9 12,479 Jan-07-2022, 04:55 PM
Last Post: bowlofred
  Subprocesses not opening File Select Dialog teut 2 4,042 Feb-22-2021, 08:07 PM
Last Post: teut
  Facing error while executing below Python code ramu4651 1 8,263 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  Noob warning: trying to use pip to install pytest and pep8 in Command Prompt adifrank 4 11,817 Dec-20-2020, 04:23 AM
Last Post: adifrank

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.