R Academy · पाठ

कार्यशील निर्देशिकाएँ और फ़ाइल पथ

विश्वसनीय क्रॉस-प्लेटफ़ॉर्म पथों के लिए getwd(), setwd() और here() का उपयोग करें।

पाठ 3, कुल 4 में से13 चरण

कार्यशील निर्देशिकाएँ और फ़ाइल पथ, CoddyKit पर R Academy का एक निःशुल्क पाठ है। यह 4 में से 3वाँ पाठ है। इस अध्ययन पथ के 3 तक कोई भी पाठ पूरा पढ़ना निःशुल्क है — इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ व्यावहारिक अभ्यास भी उपलब्ध कराता है। यह R Academy सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। R Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

कार्यशील डायरेक्टरी क्या है?

कार्यशील डायरेक्टरी वह फ़ोल्डर है जिसे R सापेक्ष पथों से फ़ाइलों का संदर्भ देते समय शुरुआती बिंदु के रूप में उपयोग करता है। जब आप पूरा पथ नहीं देते, तो R डिफ़ॉल्ट रूप से इसी स्थान में खोजता और लिखता है। किसी भी समय प्रत्येक R सत्र की ठीक एक कार्यशील डायरेक्टरी होती है।

# getwd() returns the current working directory
current_dir <- getwd()
cat('Working directory:', current_dir)

# All relative file references start from this location:
# read.csv('data.csv')         <- looks in current_dir/data.csv
# source('R/helpers.R')        <- looks in current_dir/R/helpers.R

setwd() — कार्यशील डायरेक्टरी बदलना

setwd() वर्तमान R सत्र की कार्यशील डायरेक्टरी बदलता है। यह पिछली कार्यशील डायरेक्टरी को बिना दिखाई दिए लौटाता है, जो स्थिति को पुनर्स्थापित करने के लिए उपयोगी है। ध्यान दें: प्रोजेक्ट-आधारित कार्यप्रवाहों में स्क्रिप्ट के भीतर setwd() का उपयोग हतोत्साहित किया जाता है।

# Set a new working directory
old_dir <- setwd('/tmp')
cat('Now in:', getwd(), '\n')

# Restore original directory
setwd(old_dir)
cat('Restored to:', getwd(), '\n')

# Common anti-pattern (breaks on other machines):
# setwd('/Users/alice/Desktop/my_project')  <- hardcoded!

# Better: use RStudio Projects which set wd automatically

file.path() — पोर्टेबल पथ बनाना

file.path() ऑपरेटिंग सिस्टम के लिए सही विभाजक से घटकों को जोड़कर फ़ाइल पथ बनाता है (/ Mac/Linux पर, \ Windows पर)। विभिन्न प्लेटफ़ॉर्मों के बीच संगतता के लिए स्ट्रिंग जोड़ने के बजाय हमेशा इसका उपयोग करें।

# Build a path from components
data_path <- file.path('data', 'raw', 'sales.csv')
cat('Path:', data_path)
# On Unix:    data/raw/sales.csv
# On Windows: data\raw\sales.csv

# Multiple levels
output_file <- file.path('output', '2024', 'Q1', 'report.pdf')
cat('Output:', output_file)

# Combine with getwd() for a full absolute path
full_path <- file.path(getwd(), 'data', 'sales.csv')
cat('Full path:', full_path)

normalizePath() — निरपेक्ष पथ में बदलना

normalizePath() सापेक्ष पथ को उसके मानक निरपेक्ष रूप में बदलता है और . (वर्तमान डायरेक्टरी) तथा .. (पैरेंट डायरेक्टरी) घटकों का समाधान करता है। जब आपको किसी अस्पष्टता-रहित फ़ाइल स्थान को संग्रहीत या लॉग करना हो, तब यह आवश्यक है।

# Resolve relative paths to absolute
abs_path <- normalizePath('.')
cat('Absolute cwd:', abs_path, '\n')

# Resolve a relative path
data_abs <- normalizePath(file.path('..', 'data', 'sales.csv'),
                          mustWork = FALSE)
cat('Resolved:', data_abs, '\n')

# mustWork = FALSE: do not error if file does not exist yet
# mustWork = TRUE (default): error if path does not exist

# Useful for building reliable log messages or config files
cat('normalizePath demo complete')

dirname() और basename()

dirname() किसी पथ का डायरेक्टरी वाला भाग निकालता है और basename() उसका फ़ाइल नाम वाला भाग निकालता है। ये Unix के dirname और basename शेल कमांड के R समकक्ष हैं।

path <- '/Users/alice/projects/analysis/data/sales_2024.csv'

# Extract the directory
folder <- dirname(path)
cat('Directory:', folder, '\n')
# /Users/alice/projects/analysis/data

# Extract the file name
filename <- basename(path)
cat('File name:', filename, '\n')
# sales_2024.csv

# Remove file extension
name_only <- tools::file_path_sans_ext(filename)
cat('Name only:', name_only)

file.exists() — पढ़ने से पहले जाँच

किसी फ़ाइल को पढ़ने का प्रयास करने से पहले हमेशा जाँच लें कि वह मौजूद है या नहीं, ताकि अस्पष्ट त्रुटि संदेशों से बचा जा सके। file.exists() TRUE या FALSE लौटाता है और फ़ाइलों तथा डायरेक्टरी दोनों पर काम करता है।

data_file <- file.path('data', 'sales.csv')

# Check existence before reading
if (file.exists(data_file)) {
  cat('File found, loading...\n')
  # df <- read.csv(data_file)
} else {
  cat('File not found:', data_file, '\n')
  stop('Cannot proceed without data file.')
}

# Check multiple files at once
files <- c('data/a.csv', 'data/b.csv', 'data/c.csv')
exists_vec <- file.exists(files)
cat('Files found:', sum(exists_vec), 'of', length(files))

Sys.getenv() — पर्यावरण चर के पथ

पर्यावरण चर सिस्टम-स्तरीय पथ, जैसे होम डायरेक्टरी, अस्थायी फ़ोल्डर और कस्टम ऐप पथ, संग्रहीत करते हैं। Sys.getenv() इन मानों को पढ़ता है, जिससे पथों को सीधे कोड में लिखे बिना आपका कोड अलग-अलग सिस्टमों के अनुकूल बनता है।

# Get the home directory
home_dir <- Sys.getenv('HOME')
cat('Home:', home_dir, '\n')

# Get the temp directory
tmp_dir <- Sys.getenv('TMPDIR')
cat('Temp:', tmp_dir, '\n')

# Custom environment variable (set in .Renviron)
data_root <- Sys.getenv('DATA_ROOT', unset = '/default/data')
cat('Data root:', data_root, '\n')

# Use env vars to build portable paths
config_file <- file.path(home_dir, '.config', 'myapp', 'settings.json')
cat('Config path:', config_file)

path.expand() — टिल्ड विस्तार

Unix-जैसी प्रणालियों पर टिल्ड ~ होम डायरेक्टरी का संक्षिप्त रूप है, लेकिन R के फ़ाइल फ़ंक्शन इसे हमेशा अपने-आप विस्तारित नहीं करते। path.expand() ~ को वास्तविक होम डायरेक्टरी पथ से बदल देता है।

# Expand tilde to full home path
short_path <- '~/.Rprofile'
full_path  <- path.expand(short_path)
cat('Expanded:', full_path)
# e.g. /Users/alice/.Rprofile

# Useful when passing paths to external tools or logging
config_dir <- path.expand('~/.config/R')
cat('Config dir:', config_dir)

# Can expand multiple paths at once
paths <- c('~/data', '~/output', '~/scripts')
expanded <- path.expand(paths)
cat(expanded, sep = '\n')

list.files() से फ़ाइलों की सूची बनाना

list.files() (dir() उपनाम) किसी डायरेक्टरी में मौजूद फ़ाइलों की सूची देता है; वैकल्पिक रूप से उन्हें किसी पैटर्न से फ़िल्टर किया जा सकता है और पूरे पथ भी लौटाए जा सकते हैं। किसी फ़ोल्डर में मौजूद कई फ़ाइलों को संसाधित करने की आवश्यकता होने पर यह अत्यंत उपयोगी है।

# List all files in current directory
all_files <- list.files('.')
cat('Files found:', length(all_files), '\n')

# Filter by extension
csv_files <- list.files('data', pattern = '\\.csv$', full.names = TRUE)
cat('CSV files:',  length(csv_files), '\n')

# Recursive: include subdirectories
r_files <- list.files('R', pattern = '\\.R$',
                       full.names = TRUE, recursive = TRUE)
cat('R scripts:', length(r_files))

पथ बनाना और हटाना

R में डायरेक्टरी बनाने और फ़ाइलों को प्रबंधित करने के लिए अंतर्निहित फ़ंक्शन मौजूद हैं। नए फ़ोल्डर बनाने के लिए dir.create() का उपयोग करें (नेस्टेड पथ बनाने के लिए recursive = TRUE के साथ), और अस्थायी फ़ाइलों को साफ़ करने के लिए file.remove() का उपयोग करें।

# Create a directory (will not error if it already exists)
dir.create('output/figures', recursive = TRUE, showWarnings = FALSE)
cat('Directory created\n')

# Create a temporary file
tmp_file <- tempfile(fileext = '.csv')
write.csv(data.frame(x = 1:3), tmp_file, row.names = FALSE)
cat('Temp file:', tmp_file, '\n')

# Remove the file when done
if (file.remove(tmp_file)) {
  cat('Temp file cleaned up')
}

here पैकेज — प्रोजेक्ट-सापेक्ष पथ

here पैकेज कार्यशील डायरेक्टरी की समस्या को सरलता से हल करता है। here::here() हमेशा प्रोजेक्ट रूट के सापेक्ष पथ बनाता है (जहाँ .Rproj फ़ाइल मौजूद होती है), चाहे प्रोजेक्ट के किसी भी स्थान से इसे कॉल किया जाए।

# install.packages('here')
# library(here)

# Always resolves from project root:
# here('data', 'raw', 'sales.csv')
# here('R', 'helpers.R')
# here('output', 'report.html')

# Without here: setwd() headaches when script is in a subfolder
# With here: same path works from any subfolder

# Demonstrate base-R equivalent approach:
project_root <- normalizePath(file.path(getwd(), '..'), mustWork = FALSE)
data_path    <- file.path(project_root, 'data', 'sales.csv')
cat('Data path:', data_path)

त्वरित जाँच

आपके पास 'data/raw/../clean/sales.csv' पथ स्ट्रिंग है। इसे स्वच्छ, मानक निरपेक्ष पथ में बदलने के लिए आप किस फ़ंक्शन का उपयोग करेंगे?

फ़ाइल पथ — मुख्य बातें

पुनरुत्पाद्य R स्क्रिप्ट के लिए मज़बूत फ़ाइल पथ प्रबंधन आवश्यक है:

  • getwd() / setwd() — कार्यशील निर्देशिका प्राप्त और निर्धारित करें
  • file.path('a', 'b', 'c.csv') — OS-अनुकूल पथ बनाएँ
  • normalizePath(path, mustWork = FALSE) — पथ को निरपेक्ष पथ में बदलें
  • dirname() / basename() — पथ को फ़ोल्डर और फ़ाइल में बाँटें
  • file.exists() — पढ़ने से पहले जाँच करें
  • Sys.getenv('HOME') — पर्यावरण चर के माध्यम से सिस्टम पथों तक पहुँचें
  • path.expand('~') — टिल्ड को होम निर्देशिका में विस्तृत करें
  • list.files(pattern = '\.csv$') — एक्सटेंशन के आधार पर फ़ाइलें खोजें
  • dir.create(recursive = TRUE) — नेस्टेड निर्देशिकाएँ सुरक्षित रूप से बनाएँ
  • RStudio प्रोजेक्ट में प्रोजेक्ट-सापेक्ष पथों के लिए here पैकेज का उपयोग करें
# Robust file loading pattern:
data_file <- file.path('data', 'raw', 'sales.csv')

if (!file.exists(data_file)) {
  stop(paste('Missing file:', normalizePath(data_file, mustWork = FALSE)))
}

# df <- read.csv(data_file)
cat('File path:', data_file, '\n')
cat('Full path:', normalizePath(data_file, mustWork = FALSE))
शुरुआत निःशुल्क

एआई शिक्षक के साथ R सीखें — निःशुल्क

अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।

पाठ्यक्रम
43
पाठ
159

अक्सर पूछे जाने वाले प्रश्न

क्या “कार्यशील निर्देशिकाएँ और फ़ाइल पथ” पाठ निःशुल्क है?

हाँ — R Academy अध्ययन पथ के 3 तक कोई भी पाठ, जिसमें “कार्यशील निर्देशिकाएँ और फ़ाइल पथ” भी शामिल है, यहाँ वेब पर पूरा पढ़ना निःशुल्क है। इसके बाद CoddyKit PRO हर पाठ अनलॉक करता है, साथ ही अंतर्निर्मित कोड संपादक और चौबीसों घंटे एआई शिक्षक के साथ इंटरैक्टिव अभ्यास भी उपलब्ध कराता है। R Academy पाठ्यक्रम में कुल 4 पाठ शामिल हैं।

“कार्यशील निर्देशिकाएँ और फ़ाइल पथ” में मैं क्या सीखूँगा?

विश्वसनीय क्रॉस-प्लेटफ़ॉर्म पथों के लिए getwd(), setwd() और here() का उपयोग करें। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ R Academy का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।

क्या R Academy शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?

पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर R Academy शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 3वाँ पाठ है।

“कार्यशील निर्देशिकाएँ और फ़ाइल पथ” पाठ पूरा करने में कितना समय लगता है?

CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।

क्या मैं इस R Academy पाठ में कोड लिख और चला सकता हूँ?

हाँ। हर R Academy पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।

इस पाठ्यक्रम के सभी पाठ

  1. स्क्रिप्ट लोड करने के लिए source() का उपयोग
  2. टिप्पणियाँ, शैली और पठनीयता
  3. कार्यशील निर्देशिकाएँ और फ़ाइल पथ
  4. R प्रोजेक्ट और कार्यक्षेत्र प्रबंधन
← R Academy पर वापस जाएँ